如何在清单文件中指定依赖关系,然后将其包含在.jar文件中? [英] How can I specify dependencies in the manifest file and then to include it into my .jar file?

查看:173
本文介绍了如何在清单文件中指定依赖关系,然后将其包含在.jar文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下命令生成.class文件:

  javac -cp \directoryName\external.jar myPackageDirectory\\ \\First.java myPackageDirectory\Second.java 

我需要使用 cp 在外部库(external.jar)的.jar文件的名称下,以便能够从我的代码使用这个库。






使用我的.class文件,我以下列方式生成.jar文件:

 code> jar cfm app.jar manifest.txt myPackageDirectory\ * .class 

manifest.txt 只包含一行:

 主类:myPackageName.First 






我的问题是我不是确保我能够在其他计算机上运行.jar文件。我认为是这样的,因为在编译期间我指定了外部库的.jar文件的位置。所以,我的.class文件(包含在.jar文件中)将尝试在特定目录中找到外部库的.jar文件,并且不保证外部库的.jar文件将位于同一目录在我的电脑上。






我听说上述问题可以通过


使用一个简单的文件,我
包含在我自己的jar中,而
列表依赖位置


但我不明白它是如何工作的,我需要在编译阶段指定external.jar的位置(否则编译器抱怨) p>

解决方案

首先:你似乎没有编译一个名为 MainClass 并且所有的.java文件似乎都在一个包中,所以我假设 MainClass 只是一个占位符,你在这里实际使用了正确的类名。



您需要< a href =http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html =nofollow noreferrer>指定一个 Class-Path 头将您的外部.jar提交给您的 manifest.txt 并将.jar文件与您的jar一起发送。您还需要另行执行 ,以便在编译时指定 -cp


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).

解决方案

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.

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.

这篇关于如何在清单文件中指定依赖关系,然后将其包含在.jar文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆