Reputation: 1941
I have a standalone project which is gradle based. When I do gradle build, the jar is generated under build/libs. How do I run this executable jar from command line? I tried : java -cp build/libs/foo.jar full.package.classname
but I get noClassFoundException for the classes that were imported. How do I include dependent jars as part of classpath?
Upvotes: 28
Views: 57963
Reputation: 11678
The application Gradle plugin adds a task called run to a Gradle module.
With this plugin a Gradle module named app can be executed like so:
./gradlew :app:run
Output
BUILD SUCCESSFUL in 534ms
17 actionable tasks: 1 executed, 16 up-to-date
~/Workspace/so20728621 $ ./gradlew clean :app:run
> Task :app:run
[main] INFO rbento.app.Main - Running...
The relevant settings can be found below:
~/Workspace/so20728621/settings.gradle
rootProject.name = 'so20728621'
include('app')
~/Workspace/so20728621/app/build.gradle
plugins {
id 'java'
id 'application'
}
application {
mainClass = 'rbento.app.Main'
}
Environment
Gradle: 8.1.1
JVM: 17.0.6 (Azul Systems, Inc. 17.0.6+10-LTS)
OS: Mac OS X 13.4.1 aarch64
Upvotes: 0
Reputation: 1
To run a jar in terminal, first build a jar using "gradle clean build" then , navigate to -> build -> distribution -> unzip <filename>.zip file
-> cd to unzipped file
-> cd bin
there will be 2 files - projectname & <projectname.bat> run the jar with ./projectname
If there are some args to be passed to main method use : run the jar with ./projectname params
Upvotes: -1
Reputation: 916
Add a simple task to run jar
task runJar(type: JavaExec) {
main = "-jar";
args jar.archivePath
}
UPDATE: jar.archivePath
is now deprecated, you can use jar.archiveFile.get()
task runJar(type: JavaExec) {
main = "-jar";
args jar.archiveFile.get()
}
Upvotes: 13
Reputation: 20140
Since question is marked with gradle tag I assume you want to run jar from gradle build. I also assume you use java
plugin for your gradle build.
Add next lines in your gradle:
task runFinalJar(type: JavaExec) {
classpath = files('build/libs/foo.jar')
classpath += sourceSets.main.runtimeClasspath
main = full.package.classname
}
You can now include your task to the build process:
build.dependsOn.add("runFinalJar")
Or just run it form command line:
gradle build runFinalJar
UPDATE: It is cleaner to use application plugin as Peter suggested
Upvotes: 22
Reputation: 4474
I am a newbie, so my explanations here will be simplistic. My build.gradle
file contains only one line: apply plugin: 'java'
. I have a hello world
java code in src/main/java/org/dx/test/App.java
. From the command shell, I typed: gradle build
. And then I saw this jar file magically created:
build/libs/helloworld.jar
All of the above answers did not work. What worked for me is:
java -cp build/libs/helloworld.jar org.dx.test.App
Now, I know gradle is full of sorcercy so I am sure my situation may not fully reflect your situation. The actions are did were:
build.gradle
fileApp.java
under the doctor's prescribed folder locationgradle build
Upvotes: 0
Reputation: 8497
I think that the answers go beyond what the question actually is. The question is, or can be restated as, how to run a JAR which gradle builds.
The questioner states that they've tried java -cp build/libs/foo.jar full.package.classname
to no avail.
The correct syntax is java -jar build/libs/foo.jar
, or if the JAR is right there, obviously it's then just java -jar foo.jar
as normally.
The question should be edited for clarity, IMHO.
Upvotes: 6
Reputation: 123976
Either use the application plugin to create an archive containing your code, its dependencies, and startup scripts, or create an executable fat Jar. The latter shouldn't be done in the naive way, but with the gradle-one-jar (or a similar) plugin.
Upvotes: 11