4 Replies Latest reply: Oct 31, 2011 5:21 AM by gimbal2 RSS

    java.lang.NoClassDefFoundError

    752991
      Hello All,
      I am new to Java, just trying to run a JDBC program.

      Version Details:
      Windows 7
      JDK 1.6.0_29
      Oracle 11g
      Classpath: C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib;C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib\ext;C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib\ojdbc5.jar
      Path: C:\app\owner\product\11.2.0\dbhome_1\bin;;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Java\jdk1.6.0_29\bin;C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib\ojdbc5.jar
      JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_29

      My Java program:

      import java.sql.*;
      public class TestJdbc {


      public static void main(String[] args) throws Exception {
      try{
      Class.forName("oracle.jdbc.OracleDriver");
      Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost1:ORCL","******","*****");
      System.out.println("con get--"+con.getClass());
      }
      catch(Exception ex){
      System.out.println("Error occured in opening the connection" + ex);
      }
      }

      }

      getting following error

      Exception in thread "main" java.lang.NoClassDefFoundError: TestJdbc/class
      Caused by: java.lang.ClassNotFoundException: TestJdbc.class
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      Could not find the main class: TestJdbc.class. Program will exit.

      Not sure what I am missing, getting almost same error if I run the program (compiling is working) from command prompt and also in the netbeans.

      Experts, please advise. what I am missing here?

      Thanks in advance.
        • 1. Re: java.lang.NoClassDefFoundError
          EJP
          Your command line is wrong. Also your PATH contains a JAR file, and your CLASSPATH contains directories, which are all futile. Moving this to New to Java for closer topic alignment.
          • 2. Re: java.lang.NoClassDefFoundError
            752991
            Thanks for the response.

            OK, I modified the PATH and CLASSPATH as follows

            CLASSPATH: C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib;
            PATH: C:\app\owner\product\11.2.0\dbhome_1\bin;;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Java\jdk1.6.0_29\bin;

            Now while running the program getting below error

            Error occured in opening the connectionjava.lang.ClassNotFoundException: oracle
            .jdbc.OracleDriver


            I tried the following on the command prompt

            C:\Users\owner>java oracle.jdbc.OracleDriver
            Exception in thread "main" java.lang.NoClassDefFoundError: oracle/jdbc/OracleDri
            ver
            Caused by: java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            Could not find the main class: oracle.jdbc.OracleDriver. Program will exit.


            It looks like since I changed the CLASSPATH I am getting this error.

            You mentioned that there are incorrect jars and dirs in the PATH and CLASSPATH, can you please verify the PATH and CLASSPATH are correct or not.


            Thanks
            • 3. Re: java.lang.NoClassDefFoundError
              EJP
              OK, I modified the PATH and CLASSPATH as follows
              But not correcting the problem I named in the CLASSPATH, in fact removing from it everything that was correct leaving only an error.
              It looks like since I changed the CLASSPATH I am getting this error.
              Yep.
              You mentioned that there are incorrect jars and dirs in the PATH and CLASSPATH
              That is a sloppy restatement of the precise statements I made about each. I suggest you read them again.
              • 4. Re: java.lang.NoClassDefFoundError
                gimbal2
                user6350663 wrote:
                Thanks for the response.

                OK, I modified the PATH and CLASSPATH as follows

                CLASSPATH: C:\Program Files (x86)\Java\jdk1.6.0_29\jre\lib;
                PATH: C:\app\owner\product\11.2.0\dbhome_1\bin;;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Java\jdk1.6.0_29\bin;
                Which broken article tells you to add folders of the JRE to your classpath? Don't.
                Also did that same broken article tell you to put your own jars in the Java lib directories? Don't!

                Since you are new the classpath will probably mystify you at the moment. It is wise to read all you can about it as soon as possible. Until then, make the classpath as follows:
                set CLASSPATH=.;c:\java\jars\ojdbc.jar
                A single dot and the oracle driver jar. Change the driver part to the correct path and filename; move it to somewhere OTHER than the JRE lib directories. In the future, leave the directories and files of the JDK or the JRE alone, you have no business changing any of them unless told by a piece of Oracle documentation.

                In operating system terms the dot means "the current working directory" (and .. means one directory up. You learned all that right?). So whatever directory you invoke the java or javac command in, that directory will be on the classpath. That will help Java to find your compiled class files. When you start to work with packages you'll find that this is not enough anymore; but by then you'll have read all there is to read about the classpath and you can deal with it yourself.