3

I'm trying to get my java program to talk to a MySQL DB.

So i did some reading and downloaded MySQL Connector/J. I've extracted it into my home directory ~. Here are the contents.

user@hamster:~$ ls
LoadDriver.class LoadDriver.java mysql-connector-java-5.1.18-bin.jar

The contents of LoadDriver.java are

user@hamster:~$ cat LoadDriver.java 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
 public static void main(String[] args) {
 try {
 // The newInstance() call is a work around for some
 // broken Java implementations
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 } catch (Exception ex) {
 System.out.println(ex);
 }
 }
}

The contents are the same from http://dev.mysql.com/doc/refman/5.1/en/connector-j-usagenotes-basic.html#connector-j-usagenotes-connect-drivermanager with the only change that the Exception is being printed to console in the catch block.

I compile it as follows

leonard@hamster:~$ javac LoadDriver.java

When I try to execute it, the following is the ouput.

leonard@hamster:~$ java LoadDriver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

This output is consistent with the executing command, but when trying to run it with the prescribed CLASSPATH method I run into the following issue.

leonard@hamster:~$ java -cp /home/leonard/mysql-connector-java-5.1.18-bin.jar LoadDriver
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
 at java.net.URLClassLoader1ドル.run(URLClassLoader.java:217)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: LoadDriver. Program will exit.

Am I missing something? How do I get MySQL's own code samples running.

Udo Held
12.6k11 gold badges73 silver badges94 bronze badges
asked Nov 12, 2011 at 0:22
4
  • I did try by setting the $CLASSPATH environment variable but it gives me the same error Commented Nov 12, 2011 at 0:29
  • @brunopereira81 I felt this was one time Ubuntu specific quirk... Commented Nov 12, 2011 at 0:59
  • java -cp . LoadDriver returns the same fault? Commented Nov 12, 2011 at 1:08
  • It returns java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Commented Nov 12, 2011 at 4:36

1 Answer 1

3

You need to have both the jar, and the current directory, on the classpath.

java -cp .:/home/leonard/mysql-connector-java-5.1.18-bin.jar LoadDriver
answered Nov 13, 2011 at 6:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.