如何在清单文件中指定依赖项,然后将其包含到我的 .jar 文件中?
我通过以下命令生成了 .class 文件:
javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
我需要在编译期间使用 -cp
以及“外部”库 (external.jar) 的 .jar 文件的名称,以便能够使用该库我的代码。
使用我的 .class 文件,我通过以下方式生成了 .jar 文件:
jar cfm app.jar manifest.txt myPackageDirectory\*.class
manifest.txt
仅包含一行:
Main-Class: myPackageName.First
我的问题是我不确定我是否能够运行我认为是的,因为在编译过程中我指定了外部库的 .jar 文件的位置。因此,我的 .class 文件(包含在 .jar 文件中)将尝试在特定目录中查找外部库的 .jar 文件,并且不能保证外部库的 .jar 文件位于同一目录中就像在我的电脑上一样,
我听说上述问题可以通过
我使用的 MANIFEST 文件 包括在我自己的罐子里,这将 列出依赖位置
,但我不明白它是如何工作的。我确实需要在编译阶段指定“external.jar”的位置(否则编译器会抱怨)。
I generated .class files by the following command:
javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java
I needed to use -cp
during compilation and name of .jar file of an "external" library (external.jar) to be able to use this library from my code.
Using my .class files I have generated my .jar file in the following way:
jar cfm app.jar manifest.txt myPackageDirectory\*.class
manifest.txt
contains just one line:
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.
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
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:您似乎没有编译一个名为
MainClass
的类,并且所有 .java 文件似乎都在一个包中,所以我假设MainClass
只是一个占位符,您实际上在这里使用了正确的类名。您需要指定一个
Class-Path
标头,在manifest.txt
中提及您的外部 .jar,并将 .jar 文件与您的 jar 一起交付。除了在编译时指定-cp
之外,您还需要执行此操作。First of all: you don't seem to compile a class called
MainClass
and all your .java files seem to be in a package, so I assume thatMainClass
is just a placeholder and you actually use the correct class name here.You need to specify a
Class-Path
header that mentions your external .jar to yourmanifest.txt
and deliver the .jar file together with your jar. You need to do this in addition to specifying the-cp
at compile time.根据 Joachim Sauer(非常正确)所说,有一种方法可以将依赖项 jar 打包到与您自己的代码相同的 jar 中。完成此操作的程序创建一个超级主类并操作类路径以在生成的 jar 中查找依赖的 jar。
有几个程序可以做到这一点;其中之一称为 OneJar。
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.
Several programs can do this; one of them is called OneJar.