Skip to content
Advertisement

Add a jar file to another jar file

In my project I have this code that tell class loader to load Driver.class like so:

Class.forName(org.gjt.mm.mysql.Driver);

In Eclipse it runs with no problems and I have created the Jar file of the project. But I don’t know how to insert the

mysql-connector-java-5.1.7-bin.jar

into a Jar file of my project. The folder structure look like this: this

MANIFEST file:

Manifest-Version: 1.0
Main-Class: server.MultiServer

Advertisement

Answer

I am assuming that you finally just want to run your code as

java -jar myjar.jar

There are two options.

  1. Keep mysql-connector-java-5.1.7-bin.jar next to your jar in the same folder and add classpath: mysql-connector-java-5.1.7-bin.jar to the manifest.
  2. Copy all the classes in mysql-connector-java-5.1.7-bin.jar to your jar. Do not copy the jar but the classes in the jar. This is called a fat jar or uber jar. You can automate the same using maven shade plugin.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement