When creating a jar file how do I retain the existing classpaths to other jar files?

被刻印的时光 ゝ 提交于 2019-12-11 05:23:35

问题


I recently completed a college Java assignment. The assignment utilised various other jar files, e.g. mysql connector. When I created a jar file from the Java programs in the project the connections to these other jar files were lost.

I tried various different combinations when creating the jar file and once included the mysql connector, but to no avail. I didn't expect this to work as my research had shown that usually only sound effects and images are classed as acceptable additional files.

Everything is within the same directory and all the classpaths to the various jar files are relative. My IDE is jGrasp. The jar file when executed did everything that didn't require resources other than the Java classes I had created.

Is what I was trying to achieve possible? If so how can I retain the full functionality of the system when creating a jar file?

I would be very appreciative of any help you can provide.


回答1:


You can't include jar files in other jar files, i.e., nest jar files. Also, note that the -classpath option is ignored if you execute your application using the -jar option.

Solution 1: You can edit the Class-Path option in the manifest file of the jar if you want to add other jars to the class path.

Solution 2: If you want to distribute your application as a single jar, you'll have to merge the jar files using some tool such as FatJar or OneJar.

Related questions:

  • Easiest way to merge a release into one JAR file
  • how to use one jar that depends on another jar


来源:https://stackoverflow.com/questions/5742355/when-creating-a-jar-file-how-do-i-retain-the-existing-classpaths-to-other-jar-fi

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