Ubuntu openjdk: Exception in thread "main" java.lang.NoClassDefFoundError

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值