Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running an executable jar file built from a gradle based project

Tags:

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?

like image 524
coder Avatar asked Dec 22 '13 10:12

coder


People also ask

How can I create an executable jar with dependencies using Gradle?

Right click the module > Open module settings > Artifacts > + > JAR > from modules with dependencies. Set the main class.

How do I run a .JAR file?

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]


1 Answers

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

like image 180
Eugen Martynov Avatar answered Oct 24 '22 06:10

Eugen Martynov