Page 240 in Gradle User Guide ?error? Seek clarification

Page 240 in the Gradle User Guide (PDF) states:

45.1. Usage To use the application plugin, include the following in your build script:

Example 45.1. Using the application plugin build.gradle apply plugin:‘application’

To define the main-class for the application you have to set the mainClassName property as shown below

Example 45.2. Configure the application main class build.gradle mainClassName = “org.gradle.sample.Main”

Then, you can run the application by running gradle run. Gradle will take care of building the application classes, along with their runtime dependencies, and starting the application with the correct classpath. You can launch the application in debug mode with gradle run --debug-jvm (see JavaExec.setDebug()).

I believe this is in error. I believe that mainClassName = “org.gradle.sample.Main” should be in the META-INF\MANIFEST.MF not as listed above in the build.gradle. 1) the “format” is the style used in MANIFEST.MF ( please refer to: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html )

"This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClass "

I’m confused. Did you try this and determine it didn’t work?

David, The application plugin chapter lists that the build.gradle file would be the place to put the line: mainClassName = “org.gradle.sample.Main”

however the build.gradle isn’t in that format. For instance: apply plugin: ‘java’ apply plugin: ‘application’ //apply plugin: ‘eclipse’

// tag::repositories[] repositories {

mavenCentral() } // end::repositories[]

// tag::jar[] jar {

baseName = ‘helloworld’

version = ‘0.1’ } // end::jar[]

// tag::dependencies[] dependencies {

testCompile ‘junit:junit:4.8.2’

compile “joda-time:joda-time:2.2” } // end::dependencies[]

// tag::wrapper[] task wrapper(type: Wrapper) {

gradleVersion = ‘2.1’ } // end::wrapper[]

However the format appears to resemble the MANIFEST.MF file format.

You mean that the “build.gradle” that you have for your project doesn’t include the “mainClassName” property?

If so, that is the point. You have to add the setting of that property to make the generated manifest have that value.

However, the point about needing “src/main/java” and a folder structure matching your package is very important. You should be verifying the actual contents of your jar and the manifest within it to verify your expectations.

New build.gradle: apply plugin: ‘java’ apply plugin: ‘application’ mainClassName = ‘Code.gradletest.HelloWorld’ //apply plugin: ‘eclipse’

// tag::repositories[] repositories {

mavenCentral() } // end::repositories[]

// tag::jar[] jar {

baseName = ‘helloworld’

version = ‘0.1’ } // end::jar[]

// tag::dependencies[] dependencies {

testCompile ‘junit:junit:4.8.2’

compile “joda-time:joda-time:2.2” } // end::dependencies[]

// tag::wrapper[] task wrapper(type: Wrapper) {

gradleVersion = ‘2.1’ } // end::wrapper[]

Saved and the running the build command gets:

C:\Code\gradletest>gradle build :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar UP-TO-DATE :assemble UP-TO-DATE :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build UP-TO-DATE

BUILD SUCCESSFUL

Total time: 4.126 secs

C:\Code\gradletest>gradle run :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run Error: Could not find or load main class Code.gradletest.src.main.java.HelloWorld :run FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:run’. > Process ‘command ‘C:\Java\jdk1.8.0\bin\java.exe’’ finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 4.174 secs

C:\Code\gradletest>

apply plugin: ‘java’ apply plugin: ‘application’ mainClassName = ‘Code.gradletest.src.main.java.HelloWorld’ //apply plugin: ‘eclipse’

There are a few misunderstandings here.

I see that your current directory is “C:\Code\gradletest”, and that you were attempting to define your class in “Code.gradletest…”. Those two things shouldn’t have any relation to each other.

You can define your project in “C:\Code\gradletest”, but the following file needs to exist:

“C:\Code\gradletest\src\main\java\Code\gradletest\HelloWorld.java”.

Your “mainClassName” property should be “Code.gradletest.HelloWorld”.

After you run the build, you should run “jar tvf build/libs/helloworld.jar” to verify that the jar file has your class file in the correct location in the jar file. Please verify that that is the actual path to your jar file.

You might also look in the “samples” tree in the Gradle distribution. You’ll see that the “application” sample demonstrates these features.

C:\Code\gradletest>jar tvf build/libs/helloworld-0.1.jar

0 Tue Sep 09 14:41:12 EDT 2014 META-INF/

25 Tue Sep 09 14:41:12 EDT 2014 META-INF/MANIFEST.MF

535 Tue Sep 09 14:41:12 EDT 2014 HelloWorld.class

OK I made the changes and got the above results.

BUT

Then, you can run the application by running gradle run. Gradle will take care of building the application classes, along with their runtime dependencies, and starting the application with the correct classpath.

When I run gradle run I get:

C:\Code\gradletest>gradle run :compileJava :processResources UP-TO-DATE :classes :run Error: Could not find or load main class Code.gradletest.HelloWorld :run FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:run’. > Process ‘command ‘C:\Java\jdk1.8.0\bin\java.exe’’ finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 4.759 secs

C:\Code\gradletest>

That’s because “HelloWorld.class” should not be at the root of the jar file. There should be a “Code” folder in the root, containing a “gradletest” folder, containing “HelloWorld.class”. I’m guessing you still haven’t moved “HelloWorld.java” to “src/main/java/Code/gradletest/HelloWorld.java”.

Actually I did move it yesterday afternoon and then I posted at 1855. Double checking now. Verified Its at: C:\Code\gradletest\src\main\java\Code\gradletest\HelloWorld.java

C:\Code\gradletest>gradle -v

------------------------------------------------------------ Gradle 2.1 ------------------------------------------------------------

Build time:

2014-09-08 10:40:39 UTC Build number: none Revision:

e6cf70745ac11fa943e19294d19a2c527a669a53

Groovy:

2.3.6 Ant:

Apache Ant™ version 1.9.3 compiled on December 23 2013 JVM:

1.8.0 (Oracle Corporation 25.0-b70) OS:

Windows 7 6.1 x86

C:\Code\gradletest>gradle clean :clean

BUILD SUCCESSFUL

Total time: 5.065 secs C:\Code\gradletest>gradle build :compileJava :processResources UP-TO-DATE :classes :jar :assemble :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build

BUILD SUCCESSFUL

Total time: 4.594 secs C:\Code\gradletest>gradle run :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run Error: Could not find or load main class Code.gradletest.HelloWorld :run FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:run’. > Process ‘command ‘C:\Java\jdk1.8.0\bin\java.exe’’ finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 4.162 secs

C:\Code\gradletest>jar tf build\libs\helloworld-0.1.jar META-INF/ META-INF/MANIFEST.MF HelloWorld.class

C:\Code\gradletest>

C:\Code\gradletest>dir /s

Volume in drive C is Local Disk

Volume Serial Number is 383E-B55F

Directory of C:\Code\gradletest

09/10/2014 09:03 AM

. 09/10/2014 09:03 AM

… 09/10/2014 08:46 AM

525 build.gradle 09/10/2014 09:04 AM

META-INF 09/10/2014 08:34 AM

src

1 File(s)

525 bytes

Directory of C:\Code\gradletest\META-INF

09/10/2014 09:04 AM

. 09/10/2014 09:04 AM

… 09/10/2014 09:04 AM

110 MANIFEST.MF

1 File(s)

110 bytes

Directory of C:\Code\gradletest\src

09/10/2014 08:34 AM

. 09/10/2014 08:34 AM

… 09/10/2014 08:34 AM

main

0 File(s)

0 bytes

Directory of C:\Code\gradletest\src\main

09/10/2014 08:34 AM

. 09/10/2014 08:34 AM

… 09/10/2014 08:41 AM

java

0 File(s)

0 bytes

Directory of C:\Code\gradletest\src\main\java

09/10/2014 08:41 AM

. 09/10/2014 08:41 AM

… 09/10/2014 08:41 AM

code

0 File(s)

0 bytes

Directory of C:\Code\gradletest\src\main\java\code

09/10/2014 08:41 AM

. 09/10/2014 08:41 AM

… 09/10/2014 08:41 AM

gradletest

0 File(s)

0 bytes

Directory of C:\Code\gradletest\src\main\java\code\gradletest

09/10/2014 08:41 AM

. 09/10/2014 08:41 AM

… 09/09/2014 01:30 PM

126 HelloWorld.java

1 File(s)

126 bytes

Total Files Listed:

3 File(s)

761 bytes

20 Dir(s) 102,893,006,848 bytes free

C:\Code\gradletest>