I wrote a small java program to test the postgreSql JDBC driver on X86_64 Ubuntu 12.04 LTS. Always it small program built successfully but always failed to run reporting "Exception in thread "main" java.lang.NoClassDefFoundError".
Google results directed me that I had problem with the classpath but I tried every solutions, and no luck. Run the test without specifying JDBC jar file, it reports "java.lang.ClassNotFoundException: org.postgresql.Driver" which indicates me that the run time can't find locate the JDBC driver classes. So I added the jar file in the classpath in the command line, this time it reports "Exception in thread "main" java.lang.NoClassDefFoundError". After trial and error many times, it comes that I need add acolon before the path, like this '-cp :./lib/postgresql-9.1-902.jdbc4.jar'. Or we can add it to the classpath, say "export CLASSPATH=.:./lib/postgresql-9.1-902.jdbc4.jar:/usr/lib/jvm/default-java/lib:/usr/lib/jvm/default-java/jre/lib"
Note:
1) "java.lang.ClassNotFoundException: org.postgresql.Driver" is a runtime error, the program already runs up, but failed to locate the classes it needs. Class.forName throws in this case.
2) "Exception in thread "main" java.lang.NoClassDefFoundError" is also a runtime error, but the program does not even run up and throws. The JVM could not identify the main class (entry point) of the program, it failed to load the program.
// Run the test without specifying jdbc jar file in the command line, reports:
lpadmin@LPTeam-FS:~/java$ java Test
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at Test.main(Test.java:10)
// Run the test with specifying jdbc jar file in classpath, reports:
lpadmin@LPTeam-FS:~/java$ java -cp ./lib/postgresql-9.1-902.jdbc4.jar Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Test. Program will exit.
// Run the test with specifying jdbc jar file in classpath, with colon upfront:
lpadmin@LPTeam-FS:~/java$ java Test
1 hello postgreSQL
Reference:
http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html
http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html