4 Replies Latest reply on Feb 24, 2007 4:35 AM by 843859

    stange NoClassDefFoundError...

      Hi all,

      I have a simple app running on a linux desktop that needs to connect to a MySQL database.
      I downloaded the driver (mysql-connector-java-5.0.4-bin.jar) and installed it in /usr/local/lib/mysql-connector-java-5.0.4/.

      I use ant to compile, so I added this path to the classpath :
      <property name="sqllib" value="/usr/local/lib/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar"/>
           <target name="compile">
                <mkdir dir="${classes.dir}"/>
                <javac debug="true" debuglevel="lines,vars,source" srcdir="${src.dir}" destdir="${classes.dir}" encoding="iso-8859-1">
                          <pathelement path="${classpath}"/>
                          <pathelement location="${sqllib}"/>
      In the java code, I import the following packages:
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.ResultSetMetaData;
      import java.sql.Statement;
      import java.sql.SQLException;
      import com.mysql.jdbc.Driver;
      I've tried two ways to register the driver, and both raise errors. First case :
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      Error raised :
      Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
      Second case :
      Error raised:
      java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
              at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
              at java.security.AccessController.doPrivileged(Native Method)
              at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
              at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
              at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
              at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
              at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
              at java.lang.Class.forName0(Native Method)
              at java.lang.Class.forName(Class.java:164)
      SQLException: No suitable driver
      SQLState: 08001
      VendorError: 0
      My $CLASSPATH points directly to the .jar file.
      Am I missing something??
        • 1. Re: stange NoClassDefFoundError...
          That business about compiling is irrelevant. You are getting the error at runtime. It is saying that the MySQL jar file isn't in your classpath when you run your program. Don't try and convince yourself that you are right and it is wrong. That just delays solving the problem.
          • 2. Re: stange NoClassDefFoundError...
            Okay, let me try to convince you then ;-)
            You're right, it's a runtime error. But if I set the classpath explicitly when I launch the app I still get the error:
            java -cp /usr/local/lib/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar:$CLASSPATH -jar build/jar/Coco.jar
            I've checked the path, no typo there, and everybody has read rights.
            BTW I'm not saying java's wrong, I'm just saying I have no idea where the problem comes from.
            • 3. Re: stange NoClassDefFoundError...
              Well, I found what was wrong.
              It seems java ignores any classpath you might have (as an argument or an environment variable) when you give it a .jar file to execute. The classpath has to be specified in the jar file's manifest. So I added an attribute 'Class-Path' in my build.xml file to create a manifest with the reference to the mysql driver:
              <property name="sqllib" value="/usr/local/lib/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar"/>
                   <target name="jar" depends="compile">
                        <mkdir dir="${jar.dir}"/>
                        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
                                  <attribute name="Main-Class" value="${mainclass}"/>
                                  <attribute name="Class-Path" value="${sqllib}"/>
              That's it!
              • 4. Re: stange NoClassDefFoundError...
                Hi, I am getting the same problem under windows. I definitely have the path variable pointing to the .jar Driver file location, but I can not use

                import com.mysql ...

                Which leads me to suggest the classpath is simply not opening the .jar file properly.

                Since I am not using any Ant build files, can anyone suggest a fix for this?

                Much appreciated.