开发者

class not found exception for com.mysql.jdbc.Driver not a classpath problem

开发者 https://www.devze.com 2023-01-27 21:44 出处:网络
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 \".

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.getM开发者_如何学JAVAessage());
  }
  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


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...


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.


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).


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消