File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes MySQL JDBC Driver inside JDBCExample.jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MySQL JDBC Driver inside JDBCExample.jar" Watch "MySQL JDBC Driver inside JDBCExample.jar" New topic
Author

MySQL JDBC Driver inside JDBCExample.jar

Ricardo Wagemaker
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

What is exactly the contents of the manifest file (Manifest.txt) that you're using to create the executable JAR?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ricardo Wagemaker
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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

Joined: Oct 13, 2005
Posts: 38020
    
  22
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
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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
Greenhorn

Joined: Oct 10, 2012
Posts: 16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL JDBC Driver inside JDBCExample.jar
 
Similar Threads
MySQL JDBC driver in Eclipse Ganimede
Can't find JDBC driver for project in MyEclipse
classpath JDBC driver
JDBC not included into jar file?
java.sql.SQLException: No suitable driver found