1

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.

patantdoh
  • 21
  • 2

1 Answers1

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:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • You can nest jar files *if* you use a classloader that supports it; one of the maven superjar modules does this for you, in fact. (I forget which one it is.) – Joseph Ottinger Apr 21 '11 at 11:09
  • So maven automatically includes a special class loader in the distribution? In this case it should be noted that some systems have security constraints on using a custom class loader. – aioobe Apr 21 '11 at 21:23
  • aioobe: no doubt. But in most of THOSE systems (Java EE, OSGi), an embedded classpath is already supported so you wouldn't bother with a custom classloader. – Joseph Ottinger Apr 22 '11 at 11:46