解決JDK Error occurred during initialization of VM javalangNoClassDefFoundError
因為需要使用老的JDK做測試,下載了一個J2SDK1.4的Bin安裝包,下載地址:
http://www.oracle.com/technetwork/java/javase/archive-139210.html
但是安裝和設置好環境之後運行java看版本號發現出錯:
# java -version
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
檢查PATH和CLASSPATH都設置沒問題,查看安裝目時居然發現lib目錄下沒有tools.jar和jre/lib下沒有rt.jar:
# ls -al /usr/java/j2sdk1.4.2/lib
total 8148
-rw-r--r-- 1 root root 139034 Oct 31 15:08 dt.jar
-rw-r--r-- 1 root root 1161394 Oct 31 15:08 htmlconverter.jar
-rw-r--r-- 1 root root 18381 Oct 31 15:08 ir.idl
-rw-r--r-- 1 root root 429 Oct 31 15:08 orb.idl
-rw-r--r-- 1 root root 1743428 Oct 31 15:08 tools.pack
-rwxr-xr-x 1 root root 145112 Oct 31 15:08 unpack
取而代之的是tools.pack和rt.pack。
於是使用lib目錄下自帶的uppack工具解壓這兩個jar:
#cd /usr/java/j2sdk1.4.2/lib
#unpack tools.pack tools.jar
#cd ../jre/lib
#../../unpack rt.pack rt.jar
在查看:
# ls -l /usr/java/j2sdk1.4.2/lib/tools.*
-rw-r--r-- 1 root root 5092901 Oct 31 15:29 /usr/java/j2sdk1.4.2/lib/tools.jar
-rw-r--r-- 1 root root 1743428 Oct 31 15:08 /usr/java/j2sdk1.4.2/lib/tools.pack
# ls -l /usr/java/j2sdk1.4.2/jre/lib/rt.*
-rw-r--r-- 1 root root 26782544 Oct 31 15:32 /usr/java/j2sdk1.4.2/jre/lib/rt.jar
-rw-r--r-- 1 root root 8684102 Oct 31 15:08 /usr/java/j2sdk1.4.2/jre/lib/rt.pack
在運行java一切正常了:
# java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)