问题
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