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

不问归期 提交于 2019-12-10 18:25:04

问题


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

回答1:


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




回答2:


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



回答3:


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.




回答4:


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.




回答5:


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)



来源:https://stackoverflow.com/questions/4318382/class-not-found-exception-for-com-mysql-jdbc-driver-not-a-classpath-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!