简体   繁体   中英

How to create a jar file from an eclipse java project that uses *.jar files added in “Classpath”

I have an eclipse Java project that allows connection to a database, given the url of the database and user credentials for connecting to that database. The project runs on eclipse and successfully connects to databases with the help of "ojdbc14.jar" and "mysql-connector-java-5.1.38-bin.jar" drivers.

In eclipse I have added these two drivers to my project by [right clicking in my project]->run configuration-> [add those .jar drivers to my "Classpath], so that when I run my Java project, the drivers are loaded and everything runs smoothly.

I am trying to create a jar file that includes these two drivers within the created .jar file, so that I can run through the command line my Java project the same way I'm running it on the eclipse console.

I have tried [right click project ->export -> jar-> select all files on my project-> choose main method. Etc] to create the jar file. I can open the generated jar through command prompt (Java -jar myjar.jar) and use the " main" method just like i do on eclipse but when it comes to load the driver the program stops because apparently I can't load the drivers (ojdb14.jar and mysql*.jar files).

How can I generate a *.jar file with those drivers included in it so that my program actually can find them when it is run? As of right now I have those drivers inside of a folder under the root of my project because I thought that that way they will be packed. But they can't be found when I run my.jar file in terminal.

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated 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