简体   繁体   中英

Add a jar file to another jar file

In my project I have this code that tell Class Loader to load the Driver.class 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 situation is this:

and the MANIFEST file is this :

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

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.

When you invoke your application jar add the -cp or -classpath option and provide the path to the dependent libraries, here the mysql-connector-java-5.1.7-bin.jar .

for example refer the below example

java -jar -classpath C:\\myproject\\lib\\mysql-connector-java-5.1.7-bin.jar myproject.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM