1

This is my first post and I hoping I can reach out to the group. I'm pulling my hair out with this problem.

I'm running a EC2 Ubuntu micro instance with LAMP. I'm using Java with JDBC to access the mysql database.

The issue is that the Java code keeps throwing a "ClassNotFound" Exception when I execute: Class.forName("com.mysql.jdbc.Driver");

I have installed the following:

sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysql-java

My imports in the Java file are:

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.regex.*;
import java.sql.*;
import java.util.Properties;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;

My $CLASSPATH shows:

.:/usr/share/java/mysql.jar:/usr/share/java/mysql-connector-java.jar:/usr/share/java/mysql.jar:/usr/share/java/mysql-connector-java.jar:/usr/share/java/mysql-5.1.10.jar:/usr/share/java/mysql-connector-java-5.1.10.jar

In /usr/share/java I have:

drwxr-xr-x   3 root root   4096 2012-05-25 02:01 .
drwxr-xr-x 316 root root  12288 2012-05-24 21:21 ..
-rwxrwxrwx   1 root root 448964 2009-11-23 22:38 gnome-java-bridge.jar
-rwxrwxrwx   1 root root   2621 2010-03-05 04:16 libintl.jar
lrwxrwxrwx   1 root root     31 2012-05-25 02:01 mysql-5.1.10.jar -> mysql-connector-java-5.1.10.jar
-rwxrwxrwx   1 root root 754057 2010-01-26 08:02 mysql-connector-java-5.1.10.jar
lrwxrwxrwx   1 root root     31 2012-05-25 02:01 mysql-connector-java.jar -> mysql-connector-java-5.1.10.jar
lrwxrwxrwx   1 root root     16 2012-05-25 02:01 mysql.jar -> mysql-5.1.10.jar

This is the code that always throws up the exception message to an output file:

try {
    try {
        Class.forName("com.mysql.jdbc.Driver");     
        outyyy.write("Class loaded \n");
    }
    catch (ClassNotFoundException e) {
        outyyy.write("Class not found! \n");
        outyyy.write(e.getMessage() + " \n");
    }

    this._connection = DriverManager.getConnection(url, this._user, this._pass);
    this._isConnected = true;
}
catch (Exception e) {
    this._isConnected = false;
}

I'm not sure if it's relevant but I can access and query the database just fine using PHP.

Any assistance is much appreciated.

Thanks, Andy

Raymond Tau
  • 3,429
  • 26
  • 28
user1416804
  • 11
  • 1
  • 2

1 Answers1

0

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/

Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120