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?
问题:
回答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
回答2:
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.
回答3:
Add a simple task to run jar
task runJar(type: JavaExec) {
main = "-jar";
args jar.archivePath
}
回答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.