如何在清单文件中指定依赖项,然后将其包含到我的 .jar 文件中?
我通过以下命令生成了.class文件:
I generated .class files by the following command:
javac -cp directoryNameexternal.jar myPackageDirectoryFirst.java myPackageDirectorySecond.java
我需要在编译过程中使用 -cp
并使用外部"库 (external.jar) 的 .jar 文件的名称才能从我的代码中使用此库.
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.
使用我的 .class 文件,我通过以下方式生成了我的 .jar 文件:
Using my .class files I have generated my .jar file in the following way:
jar cfm app.jar manifest.txt myPackageDirectory*.class
manifest.txt
只包含一行:
Main-Class: myPackageName.First
<小时>
我的问题是我不确定我是否能够在其他计算机上运行我的 .jar 文件.我认为是因为在编译过程中我指定了 .jar 文件的位置外部库.因此,我的 .class 文件(包含在 .jar 文件中)将尝试在特定目录中查找外部库的 .jar 文件,并且不保证外部库的 .jar 文件将在同一目录中就像在我的电脑上一样.
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
我使用 MANIFEST 文件包括在我自己的罐子里,这将列出依赖位置
usage of a MANIFEST file that I include in my own jar, and which will list dependency locations
但我不明白它是如何工作的.我确实需要在编译阶段指定external.jar"的位置(否则编译器会报错).
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).
推荐答案
首先:你似乎没有编译一个名为 MainClass
的类,而且你所有的 .java 文件似乎都在一个包,所以我假设 MainClass
只是一个占位符,你实际上在这里使用了正确的类名.
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 that MainClass
is just a placeholder and you actually use the correct class name here.
你需要指定一个Class-Path
标头 将您的外部 .jar 提到您的 manifest.txt
并将 .jar 文件与您的 jar 一起提供.除了在编译时指定 -cp
之外,您还需要这样做.
You need to specify a Class-Path
header that mentions your external .jar to your manifest.txt
and deliver the .jar file together with your jar. You need to do this in addition to specifying the -cp
at compile time.
相关文章