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:
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.
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.
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:
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.
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.
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
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.