3

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.

J.avier
  • 33
  • 5
  • 4
    Possible duplicate of [How to create a jar with external libraries included in Eclipse?](http://stackoverflow.com/questions/11033603/how-to-create-a-jar-with-external-libraries-included-in-eclipse) – randers May 05 '16 at 23:53

1 Answers1

3

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."

Tiago Bértolo
  • 3,874
  • 3
  • 35
  • 53
  • Yes, "Export as runnable Jar file" should be the answer. Though, he/she could also choose the other "library handling" options depending on how it is needed. – PendingValue May 06 '16 at 08:33