Ubuntu openjdk: Exception in thread "main" java.lang.NoClassDefFoundError
2012-06-28 10:55
465 查看
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
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
相关文章推荐
- JDK版本问题Exception in thread "main" java.lang.UnsupportedClassVersionError: Hello
- JDK版本问题Exception in thread "main" java.lang.UnsupportedClassVe
- hibernate中出现Exception in thread "main" java.lang.NoClassDefFoundError: com/opensymphony/oscache/base
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException
- hibernate Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
- Exception in thread "main" java.lang.UnsatisfiedLinkError: com.mzlr.jni.Calculater.Sum(II)I at com.
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/Synchronization
- Exception in thread "main" java.lang.NoClassDefFoundError(拿来主义)
- Exception in thread "main" java.lang.NoClassDefFoundError,找不着主方法 (eclipse 类加载器找不到)
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- Exception in thread "main" java.lang.IllegalArgumentException: Some primary key columns are missing
- 解决办法 Exception in thread "main" java.lang.NoClassDefFoundError
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Exception in thread "main" java.lang.NoClassDefFoundError: org/odmg/DSet
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决
- Eclipse运行程序提示:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Wi
- Linux 下运行Java程序报“Exception in thread "main" java.lang.NoClassDefFoundError”
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space