2010-07-08 57 views
1

我寫了一個java代碼並編譯它。 (foo1.6.class) 根據我的搜索,我的本地機器有Java 1.6,而我上傳的tomcat服務器foo1.6.class只接受版本號1.5這意味着我必須讓Java 1.5編譯?java - 編譯文件時可以選擇版本號嗎?

我認爲這是導致版本號錯誤如下所示的原因。

我的問題是,有沒有什麼辦法可以使用1.5版本編譯我的Java文件? 看着javac cmd,但似乎它不是選項的一部分。但我不認爲刪除1.6並安裝Java 1.5也不是很好的選擇。人們如何處理這種情況?

在此先感謝!

exception 

javax.servlet.ServletException: Bad version number in .class file (unable to load class resume_builder.ResumeBuilder) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:273) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 

root cause 

java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class resume_builder.ResumeBuilder) 
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1884) 
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:889) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1353) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232) 
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128) 
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66) 
    java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) 
    java.lang.Class.getDeclaredConstructors0(Native Method) 
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2357) 
    java.lang.Class.getConstructor0(Class.java:2671) 
    java.lang.Class.newInstance0(Class.java:321) 
    java.lang.Class.newInstance(Class.java:303) 
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
+0

基本上相同: http://stackoverflow.com/questions/1710933/will-jre-1-4-support-classes-compiled-with-java-1-5-1-6(注意那裏接受的答案不是最高票數,但是這兩個都給出了比本頁面當前任何答案都更好的信息。) – 2010-07-09 01:56:57

+0

@Kevin,那麼正確的操作是請求關閉作爲副本。 – 2010-07-09 03:32:32

+0

我總是對「接近完全相同」的措辭感到困擾。在那裏做什麼「確切」的詞? – 2010-07-09 15:31:07

回答

10

您可以使用javac -target 1.5 -source 1.5

只有當您還使用不高於目標的源兼容版本(源版本低於1.4的源代碼版本)時,Java編譯器才允許使用低於當前目標版本號的版本號。

+0

謝謝你們!我跑了: javac -cp。 -target 1.5 -source 1.5 /dir1/foo.java 併成功編譯。上傳到網絡服務器,並等待效果採取..... – 2010-07-08 20:39:17

+0

-1這是不安全的。看到這個問題實際上是一個重複的問題(我將在上面找到併發布上述註釋)。 – 2010-07-09 01:55:01

4
javac -help 
Usage: javac <options> <source files> 
where possible options include: 
    -g       Generate all debugging info 
    -g:none     Generate no debugging info 
    -g:{lines,vars,source}  Generate only some debugging info 
    -nowarn     Generate no warnings 
    -verbose     Output messages about what the compiler is doing 
    -deprecation    Output source locations where deprecated APIs are used 
    -classpath <path>   Specify where to find user class files and annotation processors 
    -cp <path>     Specify where to find user class files and annotation processors 
    -sourcepath <path>   Specify where to find input source files 
    -bootclasspath <path>  Override location of bootstrap class files 
    -extdirs <dirs>   Override location of installed extensions 
    -endorseddirs <dirs>  Override location of endorsed standards path 
    -proc:{none,only}   Control whether annotation processing and/or compilation is done. 
    -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process 
    -processorpath <path>  Specify where to find annotation processors 
    -d <directory>    Specify where to place generated class files 
    -s <directory>    Specify where to place generated source files 
    -implicit:{none,class}  Specify whether or not to generate class files for implicitly referenced files 
    -encoding <encoding>  Specify character encoding used by source files 
    -source <release>   Provide source compatibility with specified release 
    -target <release>   Generate class files for specific VM version 
    -version     Version information 
    -help      Print a synopsis of standard options 
    -Akey[=value]    Options to pass to annotation processors 
    -X       Print a synopsis of nonstandard options 
    -J<flag>     Pass <flag> directly to the runtime system 

請注意-source和-target參數?

+2

即使在告訴某人他們應該檢查了-help選項時,也可能會很禮貌。這不是。 – DJClayworth 2010-07-08 20:41:46

+2

如果你認爲我是不禮貌的,@DJ,當你看到互聯網上的不禮貌時,你可能想掩蓋你的眼睛。 – 2010-07-08 21:02:43

+0

我其實覺得這很有幫助。對於任何不瞭解它的人來說,第一行是一個很好的「如何」。沒有諷刺或無禮的評論。只有最薄的皮膚的人會在這裏冒犯。 – duffymo 2010-07-08 21:48:23

2

你的情況正確的答案是x4u指出的-source和-target標誌。

您問是否需要卸載Java 6才能安裝Java 5.這很少需要。 JDK提供了「javac」編譯器,並且可以安裝許多這樣的編譯器。您可以考慮在不卸載Java 6的情況下安裝Java 5 JDK,然後使用該安裝中的javac進行編譯。這將確保您的代碼將運行在Java 5運行時(這是我個人的做法)。

相關問題