简体   繁体   English

将Java库导出为.jar文件

[英]Export a java library in a .jar file

I'm still pretty new to java and I'm VERY new to exporting .jar files. 我对Java还是很陌生,而且对导出.jar文件也很陌生。 I've got a little game that I want to send to some friends and I was told in another question that I should export it to an executable jar file. 我有一个小游戏想发送给一些朋友,在另一个问题中被告知我应该将其导出到可执行的jar文件中。 Well I finally got that working on my computer but when I send it to other people it doesn't work because they don't have the library. 好吧,我终于在计算机上工作了,但是当我将其发送给其他人时,由于他们没有该库而无法工作。

I'm importing the objectdraw library and without that my program won't run at all! 我正在导入objectdraw库,没有该库,我的程序将根本无法运行!

So basically I need to find a way to export the object draw library as part of my .jar file so that they can use it too. 因此,基本上我需要找到一种方法将对象图形库导出为.jar文件的一部分,以便他们也可以使用它。 Do I simply include it in the included files part of the jar command? 我是否只是将其包含在jar命令的包含文件部分中? ex: jar cmf MANIFEST.mf Archery.jar * /System/Library/Java/Extensions/objectdraw.jar 例如:jar cmf MANIFEST.mf Archery.jar * /System/Library/Java/Extensions/objectdraw.jar

or what? 或者是什么? I'm working out of the command line right now. 我现在不在命令行中工作。

The simplest way is to send the JAR library file too and add a Class-Path entry to the manifest. 最简单的方法是也发送JAR库文件,并在清单中添加Class-Path条目。 This entry would look like: 此项看起来像:

Class-Path: objectdraw.jar

You could also set the CLASSPATH environment variable manually. 您也可以手动设置CLASSPATH环境变量。

Alternatively, you can unpack the library and add all (or just the required files) to your final jar. 或者,您可以解压缩该库并将所有(或只是所需的文件)添加到最终的jar中。 This doesn't always work though, because some libraries rely on the integrity of teir JAR file. 但是,这并不总是有效,因为某些库依赖teir JAR文件的完整性。

Finally, it is possible to include the dependency in the main JAR, but it would require a custom class loader. 最后,可以在主JAR中包含依赖项,但需要自定义类加载器。

Turns out the best way I've found to do this is to unpack the library and then put all the resulting files in with your final archive. 事实证明,我发现最好的方法是解压缩该库,然后将所有结果文件放入最终归档中。 This way it actually works on other computers. 这样,它实际上可以在其他计算机上工作。

jar xf library_wanted.jar; jar cvmf MANIFEST.mf end_result.jar *.class library_wanted/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM