简体   繁体   中英

mysql JDBC Connection NoClassDefFoundError

I am trying to test mySQL JDBC connection on a windows 2008 server. I have downloaded the JDBC driver and it created a jar file at "C:\\Program Files\\MySQL\\MySQL Connector J\\mysql-connector-java-5.1.34-bin.jar". When I am running a small program to test my jdbc connection , I am getting "NoClassDefFound" error. What am i missing here?

I did set up jdbc jar in classpath

C:\>echo %CLASSPATH%
C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

I have placed the jar in same location where I have my DBDemo.java (C:\\test)

DemoDB.java

import java.sql.*;
import java.util.Properties;
public class DBDemo
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private static final String CONNECTION =
                      "jdbc:mysql://127.0.0.1/emotherearth";
public static void main(String[] args) throws
                         ClassNotFoundException,SQLException
{
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);
    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","paulr");
    p.put("password","paulr");
    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);
    System.out.println("It works !");
    c.close();
}
}

*Error *

C:\Program Files\Java\jdk1.6.0_45\bin>java c:\test\DBDemo
Exception in thread "main" java.lang.NoClassDefFoundError: c:\test\DBDemo
Caused by: java.lang.ClassNotFoundException: c:\test\DBDemo
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
 Could not find the main class: c:\test\DBDemo.  Program will exit.
C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

Your CLASSPATH seems to be not having . that is the current working directory where the DBDemo would be there. So add . also to your CLASSPATH . After that your CLASSPATH will be like

.;C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;

EDIT

Also try extracting the contents of mysql-connector-java-5.1.34-bin.jar into a folder and change the CLASSPATH according to that.

Exception in thread "main" java.lang.NoClassDefFoundError

One of the places java tries to find your .class file is your current directory. So if your .class file is in c:\\test\\ , you should change your current directory to that.

To change your directory, type the following command at the prompt and press Enter:

cd c:\test\

Also

set CLASSPATH = .;C:\Program Files\MySQL\MySQL Connector J\mysql-connector-java-5.1.34-bin.jar;C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar;

set PATH = C:\Program Files\Java\jdk1.6.0_45\bin

Executing your program using this command should correct the problem:

c:\test>java DemoDB

这个问题可能有多种原因,但最常见的一个是类或依赖项无法相互看到,您必须验证每个类或依赖项的导入,如果您重命名了包、类或依赖项,则JVM找不到寻求阶级或依赖。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM