2009-12-15 34 views
2

在Tomcat Web應用程序中部署編譯類時出現問題:我正在部署一個要從servlet調用的類,但是當我運行該應用程序時,它告訴我一個ServletException: Error allocating the servlet instance由於UnsupportedClassVersionError: Bad version number in .class fileTomcat - UnsupportedClassException

Tomcat正在使用經理報告的Java 1.5.0_06。我的課是使用Java 1.6.0_14編譯的。在已經存在的任何類上運行javap會告訴我「主要版本46,次要版本0」,它應該是1.2.0的初始版本,並且不再可供下載。我能找到的最老的是1.2.1_004,它甚至沒有編譯。

我是否需要將我的Java版本與Tomcat環境或已經存在的類相匹配?使用更現代化的Java重新編譯整個項目對我來說目前是不可行的,儘管我很樂意這樣做。

+0

如果您使用相同版本的Java進行開發和生產,您的生活將會變得更加輕鬆。 – 2009-12-15 15:41:18

回答

4

很簡單:您使用更高版本的Java編譯器編譯應用程序,而不是使用Tomcat下的Java運行時。

更新

Java編譯器,javac的,支持的選項

-source release 
    Specifies the version of source code accepted. The following values for release are allowed: 
    1.3  the compiler does not support assertions, generics, or other language features introduced after JDK 1.3. 
    1.4  the compiler accepts code containing assertions, which were introduced in JDK 1.4. 
    1.5  the compiler accepts code containing generics and other language features introduced in JDK 5. The compiler defaults to the version 5 behavior if the -source flag is not used. 
    5 Synonym for 1.5 

...更重要的是,

-target version 
    Generate class files that will work on VMs with the specified version. The default is to generate class files to be compatible with the JDK 5 VM. When the -source 1.4 or lower option is used, the default target is 1.4. The versions supported by javac are: 
    1.1  Generate class files that will run on VMs in JDK 1.1 and later. 
    1.2  Generate class files that will run on VMs in JDK 1.2 and later, but will not run on 1.1 VMs. 
    1.3  Generate class files that will run on VMs in JDK 1.3 and later, but will not run on 1.1 or 1.2 VMs. 
    1.4  Generate class files that will run on VMs in JDK 1.4 and later, but will not run on 1.1, 1.2 or 1.3 VMs. 
    1.5  Generate class files that are compatible only with JDK 5 VMs. 
    5 Synonym for 1.5 

...這將讓您編譯特定版本的JVM的代碼。

換句話說,您可以繼續使用1.6編譯器,只需將這些選項放在它上面,並且可以生成1.5個Tomcat將能夠處理的代碼。

+0

這就像他回答他自己的問題! – Zoidberg 2009-12-15 12:14:22

+0

不知道你,但我需要所有的幫助,我可以得到:)我欣喜若狂終於碰到一個問題,我實際上有能力回答。 – 2009-12-15 12:18:53

+0

太棒了,謝謝!我會馬上嘗試。是的,我也需要我可以得到的所有幫助:D – Alessandro 2009-12-15 13:17:42

0

我是否需要我的Java版本匹配到Tomcat環境或類已經在那裏?

您需要確保您的代碼是用您正在使用的JVM支持的版本(小於或等於)編譯的;但是,不需要與Tomcat代碼庫的構建版本相同 - 兩個代碼庫可以獨立於jvm版本,只要它們都受到您所使用的jvm的支持。

0

而不是重新編譯您的源1.5是兼容的,你可以更新Tomcat正在使用的JDK到1.6。

您應該可以通過將JAVA_HOME環境變量設置爲指向Java 1.6安裝來進行更改。

+0

我很樂意將所有內容更新到更新和更好的版本,但我沒有權限這麼做。 :/ – Alessandro 2009-12-15 13:20:28

0

前段時間我得到了相同的UnsupportedClassVersionError問題。這裏的根本原因並不是我自己編譯的代碼,而是一些需要的庫,它們是用新的JDK編譯的。