Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a MySQL db from a jar file. 1

Status
Not open for further replies.

rdoor

Technical User
May 4, 2002
43
0
0
US
I built a small program for the teachers at our school and it needs to access a MySQL database. Everything was fine until I packed the class files into a jar file and tried to run it using the java -jar command and I got a "java.sql.SQLException: No suitable driver" error. The jar file sits in the same directory as the class files from which they were derived.

Here is the code:

Connection con;
public Connection getConnection() {
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e) {System.out.println("Std_Connection.init>no drivers");}
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/Standards");
}catch(SQLException sqle) {System.out.println("Std_Connection.init>Cannot make connection. sqle= "+sqle);}
return con;

Am I missing something?

Roy from Iowa
 
Are the mysql driver classes (ie the mmsql...bla.jar file) on the CLASSPATH -

if in windows type "set CLASSPATH" to view the classpath, if in linux/unix type "env |grep CLASSPATH"


Please post your classpath from these comands ...
 
Here's the output from SET:

C:\Java>set
TMP=C:\WINDOWS\TEMP
TEMP=C:\WINDOWS\TEMP
PROMPT=$p$g
winbootdir=C:\WINDOWS
COMSPEC=C:\WINDOWS\COMMAND.COM
JAVA_HOME=C:\j2sdk1.4.2
CLASSPATH=.;c:\java;c:\java\mmserver;c:\java\mmserver\madmin;c:\mysql
PATH=C:\JWSDP-1.2\JWSDP-SHARED\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\;C:\CWB;C:\J
2SDK1.4.2\BIN;.;C:\JAVA;C:\MYSQL\BIN
windir=C:\WINDOWS
BLASTER=A220 I11 D1 T4
CMDLINE=edit

What is puzzling is why it ran from the command line but not the jar file from the same directory.

Thanks!

Roy
 
Hi,

In Java, you must explicitly mention jar files on the CLASSPATH if you want them included., So for example,

set CLASSPATH=%CLASSPATH%;C:\java\mysql\mysql-connector-java-3.0.9-stable-bin.jar
 
Thanks for you help!

I added the jar file directly to the classpath, but it made no difference. The exception thrown is "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

This is what the mysql website has to say:
"Once you have un-archived the distribution archive, you can install the driver in one of two ways: Either copy the "com" and "org" subdirectories and all of their contents to anywhere you like, and put the directory holding the "com" and "org" subdirectories in your classpath, or put mysql-connector-java-[version]-bin.jar in your classpath, either by adding the FULL path to it to your CLASSPATH enviornment variable. . ."

I have both of those.

Still puzzled about why I can find the Driver class when I run from the non-jar-ed classes and not when they're in a jar file.

Roy
 
Check that you have not got two versions of the jar (or unoacked jar) on your CLASSPATH, because sometimes this can result in CNFE.

Also, how are you running this - in a webapp, or through a container like weblogic, or an IDE like netbeans/eclipse, or just on the console command line.

Try running and compiling this class from the command line :

Code:
public class Test {

	public static void main(String args[]) throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql");
		System.err.println(conn);
	}
}
 
Thanks!

I ran and compiled the Test class exactly as posted and it ran fine with output of :

com.mysql.jdbc.Connection@1d99a4d


I then added a MainClass.txt manifest file ("Main-Class: Test") and jar-ed it on the command line using "jar cmfv MainClass.txt test.jar *.*" command and ran the jar and got:

C:\Java>java -jar test.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driv
er
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at test.Test.main(Test.java:6)


Riddle me that! Thanks again for your assistance!

Roy
 
OK, also in your manifest, add the lines :

Code:
Main-Class: Test
Class-Path: C:/mysql/mm-sql-bin.jar

Where C:/mysql/mm-sql-bin.jar is your full-path to the mysql jar file.

(or unpack the mysql jar file, and add it into your own jar file also ...
 
Thanks!

It worked! I put the jar file on the classpath and a miracle happened!

Thanks again!

Roy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top