Java 运行从基于 gradle 的项目构建的可执行 jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20728621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running an executable jar file built from a gradle based project
提问by coder
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?
我有一个基于 gradle 的独立项目。当我做 gradle build 时,jar 是在 build/libs 下生成的。如何从命令行运行这个可执行 jar?我试过:java -cp build/libs/foo.jar full.package.classname
但是对于导入的类,我得到 noClassFoundException 。如何将依赖 jar 作为类路径的一部分包含在内?
回答by Eugen Martynov
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.
由于问题标有 gradle 标签,我假设您想从 gradle build 运行 jar。我还假设您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
更新:按照彼得的建议使用应用程序插件更干净
回答by Peter Niederwieser
Either use the application pluginto 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.
要么使用应用程序插件创建包含代码、其依赖项和启动脚本的存档,要么创建一个可执行的胖 Jar。后者不应该以天真的方式完成,而应该使用gradle-one-jar(或类似的)插件。
回答by Thufir
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.
恕我直言,为了清楚起见,应该编辑问题。
回答by Sylvain
Add a simple task to run jar
添加一个简单的任务来运行jar
task runJar(type: JavaExec) {
main = "-jar";
args jar.archivePath
}
回答by typelogic
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.gradle
文件只包含一行:apply plugin: 'java'
. 我hello world
在src/main/java/org/dx/test/App.java
. 在命令 shell 中,我输入:gradle build
. 然后我看到这个 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. The actions are did were:
现在,我知道 gradle 充满了魔法,所以我相信我的情况可能无法完全反映你的情况。采取的行动是:
- Create that one line
build.gradle
file - Create the
App.java
under the doctor's prescribed folder location - Type
gradle build
- 创建那一行
build.gradle
文件 App.java
在医生规定的文件夹位置下创建- 类型
gradle build