How to attach libraries to JAR file?

回眸只為那壹抹淺笑 提交于 2020-01-05 04:42:33

问题


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. What should I do?


回答1:


You could attempt building a fat jar that includes all the jars. It contains a custom class loader to load the jars referenced externally by your project.

Try using http://fjep.sourceforge.net/ plugin to build a fat jar.

You can export a java project containing jars using the File -> Export -> Other -> One Jar Exporter.

The jar thus exported works fine.




回答2:


Add external libraries to the manifest.mf:

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



回答3:


You have to keep all required jars in the classpath to run your jar. Run your jar like :

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




回答4:


Make sure that while building the jar, you include all the used libraries(include everything from class path). This issue will happen when you refer a external jar.




回答5:


You can include a classpath variable in the jar's manifest file.

JAR file classpath



来源:https://stackoverflow.com/questions/12039948/how-to-attach-libraries-to-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!