'Running an executable jar file built from a gradle based project

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?



Solution 1:[1]

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

Solution 2:[2]

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()
}

Solution 3:[3]

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.

Solution 4:[4]

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.

Solution 5:[5]

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:

  1. Create that one line build.gradle file
  2. Create the App.java under the doctor's prescribed folder location
  3. Type gradle build

Solution 6:[6]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quintin Willison
Solution 2 Harshal Parekh
Solution 3 Peter Niederwieser
Solution 4 Thufir
Solution 5 eigenfield
Solution 6 Elikill58