Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MySQL JDBC Driver inside JDBCExample.jar

 
Ricardo Wagemaker
Ranch Hand
Posts: 31
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Rather then pasting my own horrible looking code I found a perfect example and if we can make this work I will be able to make my code work too, so here is my question..

Based on http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

# Question is:

How can I make this work in my own executable jar JDBCExample.jar including the mysql-connector-java-5.1.22-bin.jar rather then in the PATH.

OK - $ javac JDBCExample.java && java -cp `pwd`/mysql-connector-java-5.1.22-bin.jar:. JDBCExample
——– MySQL JDBC Connection Testing ————
MySQL JDBC Driver Registered!
You made it, take control your database now!


OK - jar cvfm JDBCExample.jar Manifest.txt JDBCExample.class mysql-connector-java-5.1.22-bin.jar
added manifest
adding: JDBCExample.class(in = 1345) (out= 809)(deflated 39%)
adding: mysql-connector-java-5.1.22-bin.jar(in = 832960) (out= 797090)(deflated 4%)

OK - $ java -jar JDBCExample.jar
——– MySQL JDBC Connection Testing ————
MySQL JDBC Driver Registered!
You made it, take control your database now!

Unfortunately it is still using the local file in the PATH, rather than the file I added in the JAR previously, if I move mysql-connector-java-5.1.22-bin.jar from the PATH it failed to find the driver:

BAD - $java -jar JDBCExample.jar
——– MySQL JDBC Connection Testing ————
Where is your MySQL JDBC Driver?


Any additional help would be greatly appreciated as I just want to create my own JAR file & include & use the mysql-connector-java-5.1.22-bin.jar file from within the JDBCExample.jar

Many thanks in advance.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is exactly the contents of the manifest file (Manifest.txt) that you're using to create the executable JAR?
 
Ricardo Wagemaker
Ranch Hand
Posts: 31
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:What is exactly the contents of the manifest file (Manifest.txt) that you're using to create the executable JAR?


Hello,

Thank you for showing a interrest in my question, here it is:

Main-Class: JDBCExample
Name: JDBCExample
Class-Path: mysql-connector-java-5.1.22-bin.jar
Sealed: true

This allows me to run: $ java -jar JDBCExample.jar
——– MySQL JDBC Connection Testing ————
MySQL JDBC Driver Registered!
You made it, take control your database now!

But as soon as I remove it from the PATH is will stop working and I want it to use the mysql-connector-java-5.1.22-bin.jar that I added to the JDBCExample.jar file.

Many Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48975
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean about removing from your PATH? That manifest file appears to work, and that looks like the correct way to use your MySQL connector, so why not stick to that?
 
Ricardo Wagemaker
Ranch Hand
Posts: 31
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do you mean about removing from your PATH? That manifest file appears to work, and that looks like the correct way to use your MySQL connector, so why not stick to that?


Hi this all works as long as I have the mysql-connector-java-5.1.22-bin.jar located in my filesystem, I want to be able to use mysql-connector-java-5.1.22-bin.jar that is located inside JDBCExample.jar

So rather than JDBCExample.class calling "Drive" inside mysql-connector-java-5.1.22-bin.jar

I want JDBCExample.class calling "Drive" inside mysql-connector-java-5.1.22-bin.jar that is inside JDBCExample.jar !

I hope this makes sense.

Many Thanks

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not allow nesting JAR files. It will not work if you put the MySQL driver JAR inside your own JAR file, for example. If your manifest file looks like that, the two JAR files have to be in the same directory for it to work.

There are tools such as One-JAR which can help you achieve what you want, to package everything in a single JAR file.
 
Ricardo Wagemaker
Ranch Hand
Posts: 31
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Java does not allow nesting JAR files. It will not work if you put the MySQL driver JAR inside your own JAR file, for example. If your manifest file looks like that, the two JAR files have to be in the same directory for it to work.

There are tools such as One-JAR which can help you achieve what you want, to package everything in a single JAR file.


Many Thanks for your help at least now I know why i can't get it to work, I will download the One-JAR at home tonight and try it out, it looks easy enough.

Many Thanks
 
Ricardo Wagemaker
Ranch Hand
Posts: 31
1
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to do what I want using Eclipse --->

1 - Added folder RES to my project
2 - Added RES to Class folder in the Build PATH
3 - Copy the mysql-connector-java-5.1.22-bin.jar to RES
4 - In Java Build Path Add Jar's selected the mysql-connector-java-5.1.22-bin.jar from RES
5 - In Navigator -> export Project --> Runnable Jar File --> In Library Handling --> Extract required library into generated JAR

This has done exactly what I wanted it's all working now and from now on I will only use Eclipse rather than coding in VI

Many Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic