Java – gradle output jar does not have a main class

I have the following simple build Gradle file:

apply plugin: 'application'
apply plugin: 'java'

mainClassName = 'com.kurtis.HelloGradle'

The following individual java files are located in Src / main / Java / COM / kurtis / hellogradle java:

package com.kurtis;

public class HelloGradle {

    public static void main(String[] args) {

        System.out.println("Hello gradle");
    }

}

However, if I run the gradle build, I will get a jar without the main class setting in the build / lib directory Its manifest file does not have a main class entry Why?

Solution

This is due to the way the application plug - in works Mainclassname is an attribute used by the application plug-in. When it creates an executable jar, it does not need to have a main class in the list because it does not use it

If you need to have a main class in your jar, you must provide its vie jar configuration with the Java plug-in, as shown below:

jar {
  manifest {
    attributes(
      'Main-Class': 'com.kurtis.HelloGradle'
    )
  }
}

Otherwise, just use the executable generated by the application plug-in, whether by running the task or through the distribution it can create for you, and use the script to run the application and all its dependencies

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>