11

I have the following simple build.gradle file:

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

mainClassName = 'com.kurtis.HelloGradle'

And the following single java file located at 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 gradle build I get a jar in the build/lib directory that has no main class set. It's manifest file has no Main-Class entry. Why is this?

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102

1 Answers1

18

It's due to the way application plugin works. mainClassName is a property used by application plugin, when it creates an executable jar, it doesn't need to have a Main-Class in the manifest, since it doesn't use it.

If you need to have a Main-Class in your jar, then you have to provide it vie jar configuration of the java plugin as follows:

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

Otherwise, just use an executable, which is generated by application plugin, whether via run task or via distributions it can create for you, with scripts to run your application and all it's dependencies.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 1
    This was a great explanation. Cleared up a lot of things for me. Thanks! – Kurtis Nusbaum Mar 29 '17 at 17:01
  • 1
    thanks! No I'm wondering what I'd need the application plugin for.. I'll just use the java plugin and this. Sufficient to start it with java -jar x.jar – tObi Jan 31 '19 at 13:25
  • Would be nice to point out that, nowadays, if you are using the `application` plugin and correctly setting `mainClassName`, there is no need to specify `Main-Class` inside `jar`'s manifest attributes. – x80486 Jun 28 '19 at 19:10