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


uang-pei-ji@huang-pei-ji:~$ scala -version Scala code runner version 2.11.12 -- Copyright 2002-2017, LAMP/EPFL huang-pei-ji@huang-pei-ji:~$ scala Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 21.0.8). Type in expressions for evaluation. Or try :help. Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/fusesource/jansi/AnsiOutputStream at jline.internal.Ansi.stripAnsi(Ansi.java:28) at jline.console.ConsoleReader.setPrompt(ConsoleReader.java:499) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2425) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2378) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2366) at scala.tools.nsc.interpreter.jline.InteractiveReader.readOneLine(JLineReader.scala:59) at scala.tools.nsc.interpreter.InteractiveReader$class.readLine(InteractiveReader.scala:38) at scala.tools.nsc.interpreter.jline.InteractiveReader.readLine(JLineReader.scala:27) at scala.tools.nsc.interpreter.SplashReader.readLine(InteractiveReader.scala:142) at scala.tools.nsc.interpreter.SplashLoop.run(InteractiveReader.scala:71) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.lang.ClassNotFoundException: org.fusesource.jansi.AnsiOutputStream at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ... 11 more Failed to initialize compiler: NoClassDefFoundError. This is most often remedied by a full clean and recompile. Otherwise, your classpath may continue bytecode compiled by different and incompatible versions of scala. java.lang.NoClassDefFoundError: org/fusesource/jansi/AnsiOutputStream at jline.internal.Ansi.stripAnsi(Ansi.java:28) at jline.console.ConsoleReader.setPrompt(ConsoleReader.java:499) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2425) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2378) at jline.console.ConsoleReader.readLine(ConsoleReader.java:2366) at scala.tools.nsc.interpreter.jline.InteractiveReader.readOneLine(JLineReader.scala:59) at scala.tools.nsc.interpreter.InteractiveReader$class.readLine(InteractiveReader.scala:38) at scala.tools.nsc.interpreter.jline.InteractiveReader.readLine(JLineReader.scala:27) at scala.tools.nsc.interpreter.ILoop.readOneLine(ILoop.scala:413) at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:425) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:993) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:891) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:891) at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97) at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:891) at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74) at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87) at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98) at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103) at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) Caused by: java.lang.ClassNotFoundException: org.fusesource.jansi.AnsiOutputStream ... 20 more
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值