如何在ubuntu中运行具有大量jar依赖项的java文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22795183/
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
How to run a java file with a lot of jars dependencies in ubuntu
提问by user1708240
I have a java class which has almost 12 jar file dependencies and i am using ubuntu 12.10 . I need to know how to run this java application because every time i run it , it gives me errors as "symbols not found". I have all jar files in a folder called libs. and i have tried these commands but none of these gives me some succesful result.I have flights.java class in test directory and libs directory is inside test directory.Currently i am in test directory
我有一个 java 类,它有近 12 个 jar 文件依赖项,我正在使用 ubuntu 12.10 。我需要知道如何运行这个 Java 应用程序,因为每次我运行它时,它都会给我错误,如“找不到符号”。我将所有 jar 文件放在一个名为 libs 的文件夹中。我已经尝试过这些命令,但这些命令都没有给我一些成功的结果。我在 test 目录中有 flight.java 类,而 libs 目录在 test 目录中。目前我在 test 目录中
javac -cp "/home/ubuntu/test/libs/*.jar" flights.java
javac -cp "/home/ubuntu/test/libs/*.jar" flight.java
javac -cp '/home/ubuntu/test/libs/*.jar' flights.java
javac -cp '/home/ubuntu/test/libs/*.jar' flight.java
回答by Jigar Joshi
if you have single class in your app called flights.java
and all of your required jar are located at /home/ubuntu/test/libs/
then use this
如果您的应用程序中有一个类被调用flights.java
并且您需要的所有 jar 都位于,/home/ubuntu/test/libs/
则使用它
javac -cp '.:/home/ubuntu/test/libs/*.jar' flights.java
and to run
并运行
java -cp '.:/home/ubuntu/test/libs/*.jar' flights
better to just pack dependency and app in to a single jar and make it launchable and runnable jar
最好将依赖项和应用程序打包到一个 jar 中,并使其可启动和可运行 jar
回答by J.J
12 jars is not a very large number. Why not just append all the jars on the classpath?
12 罐并不是一个很大的数字。为什么不直接在类路径上附加所有 jars?
Alternatively, you can create another jar and specify all the jars in Class-Path variable in that jar's MANIFEST.MF and then add this single jar to your classpath.
或者,您可以创建另一个 jar 并在该 jar 的 MANIFEST.MF 中的 Class-Path 变量中指定所有 jar,然后将此单个 jar 添加到您的类路径中。
EDIT:
编辑:
Here is how I would do it. Create a MANIFEST.MF file with content similar to this:
这是我将如何做到的。创建一个 MANIFEST.MF 文件,内容类似于:
Manifest-Version: 1.0
Archiver-Version: whatever
Created-By: whatever
Built-By: author-name
Build-Jdk: 1.6.0_34
Class-Path: jar1.jar jar2.jar jar3.jar
replace jar1.jar
with the actual file names of the jar.
替换jar1.jar
为 jar 的实际文件名。
Then you can create a jar with command : jar cvf test.jar -m ./MANIFEST.MF
.
然后你可以用命令创建一个 jar : jar cvf test.jar -m ./MANIFEST.MF
。
Now when you are using it on classpath use it like java -jar xyz.jar class-name
现在,当您在类路径上使用它时,请使用它 java -jar xyz.jar class-name