简体   繁体   English

如何将库附加到JAR文件?

[英]How to attach libraries to JAR file?

It works fine when compiling project, but after exporting it to a runnable jar and launching, it can't find external files and throws an error. 它在编译项目时工作正常,但在将其导出到可运行的jar并启动后,它无法找到外部文件并引发错误。 What should I do? 我该怎么办?

将外部库添加到manifest.mf

Class-Path: . MyApp_lib/extlib.jar MyApp_lib/extlib2.jar ...

You could attempt building a fat jar that includes all the jars. 你可以尝试建造一个包含所有罐子的fat jar罐子。 It contains a custom class loader to load the jars referenced externally by your project. 它包含一个自定义类加载器,用于加载项目外部引用的jar。

Try using http://fjep.sourceforge.net/ plugin to build a fat jar. 尝试使用http://fjep.sourceforge.net/插件来构建一个胖罐。

You can export a java project containing jars using the File -> Export -> Other -> One Jar Exporter . 您可以使用File -> Export -> Other -> One Jar Exporter包含jar的java项目。

在此输入图像描述

The jar thus exported works fine. 这样输出的罐子工作得很好。

You have to keep all required jars in the classpath to run your jar. 你必须在类路径中保留所有必需的jar来运行你的jar。 Run your jar like : 运行你的罐子像:

java -cp extlib/* -jar yourjar.jar OR java -cp lib1.jar:lib2.jar:.. -jar yourjar.jar java -cp extlib/* -jar yourjar.jarjava -cp lib1.jar:lib2.jar:.. -jar yourjar.jar

Make sure that while building the jar, you include all the used libraries(include everything from class path). 确保在构建jar时,包含所有使用的库(包括类路径中的所有内容)。 This issue will happen when you refer a external jar. 引用外部jar时会发生此问题。

You can include a classpath variable in the jar's manifest file. 您可以在jar的清单文件中包含一个classpath变量。

JAR file classpath JAR文件类路径

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

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