3

I've been working on this for weeks, and now I'm just running in circles. I'm getting the runtime error: " class not found exception in [class] while getting connection com.mysql.jdbc.Driver ".

The connection function is as follows:

/**
 * @return a MySQL connection to the server as specified above 
 */
private static Connection getConnection() {
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(url, username, password);
  } catch (ClassNotFoundException e) {
    System.err.println("class not found exception in [class] while getting connection " + e.getMessage());
  } catch (SQLException e) {
    System.err.println("sql exception in [class] while getting connection " + e.getMessage());
  }
  return con;
} //end getConnection

I have set the $CLASSPATH a hundred times a hundred ways, and still no luck. This is what it looks like now:

# echo $CLASSPATH
:/usr/share/java/mysql-connector-java-5.1.13-bin.jar:/usr/share/java/mysql-connector-java-3.0.17-ga-bin.jar:

Both of those files exist and have the Driver.class file in them in the right place.

I even tried importing the class into my code before exporting the jar, but that caused a world of other problems.

I'm using Eclipse 3.5.2 and the server is running Fedora 13 x86_64

Please help.

edit: I'm running the code remotely on my server with the command:

# java -jar program.jar
4
  • 2
    Why do you say "not a classpath problem"? It very definitely is. Commented Nov 30, 2010 at 20:46
  • 1
    did you add the jars to your eclipse classpath? Commented Nov 30, 2010 at 20:46
  • I said not classpath because most threads on the issue are full of people stating "just add it to the classpath" which doesn't help, since I think I have done that. Yes, I do think I did it wrong, or did it right but have another issue with the classpath, but I did try. Commented Nov 30, 2010 at 21:00
  • How do i add it to the eclipse classpath? Commented Nov 30, 2010 at 21:01

5 Answers 5

4

Assuming you run it under Eclipse, try to add a reference to the jars in the Eclipse Run setting dialog. Menu Run -> Run Configuration -> you project -> Classpath tab -> User Entries -> Add Jar...

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, forgot to say I'm only coding in eclipse, not running in it. I'm running the code with "# java -jar program.jar" in the command line on the server.
4

If you are deploying on Apache Tomcat, place your JDBC connector jar file (mysql-connector-java-5.0.8-bin.jar) on lib folder of Tomcat home \apache-tomcat-6.0.29\lib\.

Some threads say its <tomcat_home>\common\lib\, but there is no common directory found on 6.0 in my case.

And don't forget to restart the server.

Comments

4

There is an easy way to solve this problem in Eclipse:

  1. Right Click on your project
  2. Select "Properties"
  3. Select "Deployment Assembly"
  4. Click Add -> "Java Build Path Entries"
  5. Select your user library that contains your database connector (for example, mysql-connector-java-5.1.20.jar).

Comments

1

Problem has been fixed: I had to edit the manifest file to include the ClassPath: variable to the files I needed outside of eclipse, because eclipse does not allow an easy way to do it. When using JAR files, the classpath of the server doesn't matter, only that of the jar.

Comments

0

I found that I had to goto

Package Properties (Right click on project/package name and find Properties in the sub menu it might be at the bottom).

Java Build Path Click the tab 'Order and Export'

Then tick the empty check box next to the library I wanted to include in the jar export. (In my case it was ojdbc6.jar)

Comments

Your Answer

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.