coder
coder

Reputation: 1941

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?

Upvotes: 28

Views: 57963

Answers (7)

rbento
rbento

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

anant singh
anant singh

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

Sylvain
Sylvain

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

Eugen Martynov
Eugen Martynov

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

daparic
daparic

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:

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

Upvotes: 0

Thufir
Thufir

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

Peter Niederwieser
Peter Niederwieser

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

Related Questions