java 如何在清单文件中指定依赖项,然后将其包含到我的 .jar 文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2592115/
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 can I specify dependencies in the manifest file and then to include it into my .jar file?
提问by Roman
I generated .class files by the following command:
我通过以下命令生成了 .class 文件:
javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
I needed to use -cpduring compilation and name of .jar file of an "external" library (external.jar) to be able to use this library from my code.
我需要-cp在编译期间使用“外部”库 (external.jar) 的 .jar 文件的名称,以便能够从我的代码中使用该库。
Using my .class files I have generated my .jar file in the following way:
使用我的 .class 文件,我通过以下方式生成了我的 .jar 文件:
jar cfm app.jar manifest.txt myPackageDirectory\*.class
manifest.txtcontains just one line:
manifest.txt只包含一行:
Main-Class: myPackageName.First
My problem is that I am not sure that I will be able to run my .jar file on other computers.I think so because during the compilation I specified the location of the .jar file of the external library. So, my .class files (included into the .jar file will try to find the .jar file of the external library in a specific directory and there is no guaranty that that the .jar file of the external library will be in the same directory as on the my computer.
我的问题是我不确定我是否能够在其他计算机上运行我的 .jar 文件。我这么认为是因为在编译过程中我指定了外部库的 .jar 文件的位置。因此,我的 .class 文件(包含在 .jar 文件中将尝试在特定目录中查找外部库的 .jar 文件,并且不能保证外部库的 .jar 文件将位于同一目录中就像在我的电脑上一样。
I heard that the above problem can be solved by a
听说上面的问题可以通过
usage of a MANIFEST file that I include in my own jar, and which will list dependency locations
使用包含在我自己的 jar 中的 MANIFEST 文件,该文件将列出依赖项位置
but I do not understand how it works. I do need to specify location of the "external.jar" at the compilation stage (otherwise the compiler complains).
但我不明白它是如何工作的。我确实需要在编译阶段指定“external.jar”的位置(否则编译器会抱怨)。
采纳答案by Joachim Sauer
First of all: you don't seem to compile a class called MainClassand all your .java files seem to be in a package, so I assume that MainClassis just a placeholder and you actually use the correct class name here.
首先:您似乎没有编译一个名为的类,MainClass并且您的所有 .java 文件似乎都在一个包中,所以我认为这MainClass只是一个占位符,您实际上在这里使用了正确的类名。
You need to specify a Class-Pathheaderthat mentions your external .jar to your manifest.txtand deliver the .jar file together with your jar. You need to do this in additionto specifying the -cpat compile time.
您需要指定一个Class-Path标头,其中提到您的外部 .jarmanifest.txt并将 .jar 文件与您的 jar 一起交付。除了指定-cp编译时,您还需要执行此操作。
回答by Carl Smotricz
Further to what Joachim Sauer (very correctly) says, there is a way to pack your dependency jars into the same jar as your own code. The programs that accomplish this create a super-main class and manipulate the classpath to find the dependent jars in your resulting jar.
除了 Joachim Sauer(非常正确)所说的之外,还有一种方法可以将您的依赖 jar 打包到与您自己的代码相同的 jar 中。完成此操作的程序会创建一个超级主类并操纵类路径以在生成的 jar 中查找相关的 jar。
Several programs can do this; one of them is called OneJar.
有几个程序可以做到这一点;其中之一称为OneJar。

