Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems using CLASSPATH for mysql-driver

843859Jul 27 2008 — edited Jan 20 2015
I'm by no means a java expert, so please bear with me. My setup is Ubuntu 8.04, running

myuser@mycomputer:~$ java -version
java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

and

myuser@mycomputer:~$ javac -version
javac 1.6.0_06

I've installed the appropriate packages (i.e. libmysql-java) to get the MySQL Connector/J installed at /usr/share/java/mysql-connector-java-5.1.5.jar

My program goes as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class test_mysql {

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException {

		System.out.println("Mysql-test.");

		try {

			// The newInstance() call is a work around for some
			// broken Java implementations
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			System.out.println("Driver found.");

		} catch (Exception ex) {

			// handle the error
			System.out.println("Driver not found.");

		}

	} 

}
I'm using Eclipse, so first I ran it with the correct external jar (/usr/share/java/mysql-connector-java-5.1.5.jar) included in the "Libraries" section in the "Project properties" dialog. Works like a charm - even reporting that the driver is found.

Then I decided that I wanted to be able to use the CLASSPATH environment variable and call the java compiler from a terminal instead. I ran

javac -classpath /usr/share/java/mysql-connector-java-5.1.5.jar test_mysql.java

without errors. Then

java -classpath /usr/share/java/mysql-connector-java-5.1.5.jar test_mysql
Exception in thread "main" java.lang.NoClassDefFoundError: test_mysql
Caused by: java.lang.ClassNotFoundException: test_mysql
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

Here's my questions.

1: Anyone know why my program runs fine in Eclipse, but not in terminal? Btw: I've set up Eclipse to use the sun-6-java compiler and jvm, so it shouldn't give different results.
2: Do I have to include the classpath switch both when I call the compiler and when I invoke the jvm? (I know it's possible to set it as an environment variable, I'm just not there yet. Got to make it work as a switch first.)

Thanks in advance!

Regards auditor :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2008
Added on Jul 27 2008
2 comments
243 views