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?
Right click the module > Open module settings > Artifacts > + > JAR > from modules with dependencies. Set the main class.
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 …]
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With