Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am attempting to connect to my local MySQL server with the following code:

dbURL = "jdbc:mysql://localhost:3306:/" + dbname;
try{
    Class.forName("com.mysql.jdbc.Driver");
    try{
        con = DriverManager.getConnection(dbURL, dbuser, dbpass);
    } catch (SQLException ex){
        System.out.println("ERROR: Could not connection to SQL DB");
        con = null;
    }
} catch (ClassNotFoundException e){
    System.out.println("Error: ");
    e.printStackTrace();
}

I then get

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I understand that Java cannot find the proper driver for connecting the Java environment to the MySQL database. This is being compiled on a Windows 7 system and ported over to an Ubuntu 11.04 system.

Is there a particular way I can run the Java program with a particular classpath such as:

java -cp /usr/share/java/mysql-connector-java.jar program.jar

That didn't work when I tried it.

share|improve this question
up vote 3 down vote accepted

In case of JARs, the -cp and -classpath arguments and the %CLASSPATH% environment variable are ignored. Instead, the classpath has to be specified in the Class-Path entry of JAR's own /META-INF/MANIFEST.MF file. It can be a path relative to the JAR itself. E.g. in the same folder or in a /lib subfolder.

The below example assumes the driver to be in the same folder as the JAR.

Class-Path: mysql-connector-java.jar

(make sure that the MANIFEST.MF file has a blank line at the end)

See also:

share|improve this answer
    
This worked great Thanks. – Blackninja543 Dec 23 '11 at 23:23
    
You're welcome. Since you're new here, please don't forget to mark the answer accepted whenever it helped (most) in solving the problem. See also meta.stackexchange.com/questions/5234/… – BalusC Dec 23 '11 at 23:27

No matter where it's compiled, it will run smoothly in the same JVM implementation.

All you have to do is to properly include a JDBC driver connector in your classpath. Try putting mysql-connector-java.jar into the same directory with your program.

If you unzip this jar file, does it contain com/mysql/jdbc/Driver.class? If not, try to download jdbc driver implementation from the mysql web-site.

share|improve this answer
    
@Blackninja543 Also, as of JDBC 4.0 there is no need to load the driver in order to use it. The JDBC Specification section 9.2.1 explains how compatible drivers now support service provider mechanism. If your jdbc jar file contains META-INF/services/java.sql.Driver file with the driver class, it means you need not to load the driver in order to use. You just need in your class path. – Edwin Dalorzo Dec 23 '11 at 22:39

Install MySQL connector for JAVA

sudo apt-get install libmysql-java

Set classpath

export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

Source: http://marksman.wordpress.com/2009/03/01/setting-up-mysqljdbc-driver-on-ubuntu/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.