[英]Running an executable jar file built from a gradle based project
I have a standalone project which is gradle based.我有一个基于 gradle 的独立项目。 When I do gradle build, the jar is generated under build/libs.
当我进行 gradle 构建时,在 build/libs 下生成了 jar。 How do I run this executable jar from command line?
如何从命令行运行此可执行文件 jar? I tried:
java -cp build/libs/foo.jar full.package.classname
but I get noClassFoundException for the classes that were imported.我试过:
java -cp build/libs/foo.jar full.package.classname
但我得到了导入类的 noClassFoundException。 How do I include dependent jars as part of classpath?如何将相关的 jars 作为类路径的一部分包含在内?
Since question is marked with gradle tag I assume you want to run jar from gradle build.由于问题标有 gradle 标签,我假设您想从 gradle build 运行 jar。 I also assume you use
java
plugin for your gradle build.我还假设您使用
java
插件进行 gradle 构建。
Add next lines in your gradle:在你的 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更新:按照彼得的建议使用应用程序插件更干净
Either use the application plugin to create an archive containing your code, its dependencies, and startup scripts, or create an executable fat Jar.要么使用应用程序插件创建包含代码、其依赖项和启动脚本的存档,要么创建一个可执行的胖 Jar。 The latter shouldn't be done in the naive way, but with the gradle-one-jar (or a similar) plugin.
后者不应该以天真的方式完成,而应该使用gradle-one-jar (或类似的)插件。
Add a simple task to run jar添加一个简单的任务来运行jar
task runJar(type: JavaExec) {
main = "-jar";
args jar.archivePath
}
UPDATE : jar.archivePath
is now deprecated, you can use jar.archiveFile.get()
更新:
jar.archivePath
现在已弃用,您可以使用jar.archiveFile.get()
task runJar(type: JavaExec) {
main = "-jar";
args jar.archiveFile.get()
}
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.
问题是,或者可以重申为,如何运行 gradle 构建的 JAR。
The questioner states that they've tried java -cp build/libs/foo.jar full.package.classname
to no avail.提问者说他们试过
java -cp build/libs/foo.jar full.package.classname
无济于事。
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.正确的语法是
java -jar build/libs/foo.jar
,或者如果 JAR 就在那里,显然它只是java -jar foo.jar
正常。
The question should be edited for clarity, IMHO.恕我直言,为了清楚起见,应该编辑问题。
I am a newbie, so my explanations here will be simplistic.我是新手,所以我在这里的解释会很简单。 My
build.gradle
file contains only one line: apply plugin: 'java'
.我的
build.gradle
文件只包含一行: apply plugin: 'java'
。 I have a hello world
java code in src/main/java/org/dx/test/App.java
.我在
src/main/java/org/dx/test/App.java
有一个hello world
java 代码。 From the command shell, I typed: gradle build
.在命令 shell 中,我输入:
gradle build
。 And then I saw this jar file magically created:然后我看到这个 jar 文件神奇地创建了:
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.现在,我知道 gradle 充满了魔法,所以我相信我的情况可能无法完全反映你的情况。 The actions are did were:
采取的行动是:
build.gradle
filebuild.gradle
文件App.java
under the doctor's prescribed folder locationApp.java
gradle build
gradle build
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要在终端中运行 jar,首先使用“gradle clean build”构建一个 jar,然后导航到 -> 构建 -> 分发 ->
unzip <filename>.zip file
-> cd 到解压缩文件 -> cd bin
会有 2文件 - projectname & <projectname.bat> 使用 ./projectname 运行 jar
If there are some args to be passed to main method use: run the jar with./projectname params如果有一些参数要传递给主要方法使用:运行 jar with./projectname params
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.