简体   繁体   中英

Java - Pack referenced libraries into jar file and re-use

When I create a new Java project that use third party dependencies such as MySQL-Connector, and export this into a jar file to use in another Java project, how do I avoid having to add (again) the MySQL-Connector library to the new projects build path?

What I would like to achieve is to pack down referenced libraries with the jar file, so when I add it to a new project its dependencies are already met within the jar "container" and I can avoid

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

>>Example<<

When working with my SQL database I first need the MySQL-connector library, this of course have the prepared-statements methods, update query and so on. But it doesn't have (as far as I know) for example a method for multiple query updates or updates with an unknown number of ?,?,? arguments etc. So for my own part I have created a SQL manager project that contains a class that contains all the SQL methods I need.

I then import the MySQL-connector -> into the manager project and I would like to export this entire project as a self-contained jar file so when I import the manager jar into a project that needs a database, I don't need to both import the manager jar and the connector, but just the manager that than contains the connector.

Here is what to do technically (in Eclipse, as you tagged it):

  1. Open your project`s properties and click on "Java Build Path".

  2. Switch to the tab named "Order and Export".

  3. Make sure to check the checkboxes for all the jars you want to have in your jar.

But there is one more thing to do: you should carefully check the licence of the jars you want to include in this way because you may not be allowed to do so.

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