java - 编译文件时是否可以选择版本号?
我写了一段java代码并编译了它。 (foo1.6.class) 根据我的搜索,我的本地机器有Java 1.6,而我上传的foo1.6.class的tomcat服务器只接受版本号1.5,这意味着我必须有Java 1.5才能编译?
我相信这是抛出错误版本号错误的原因,如下所示。
我的问题是,有什么方法可以使用 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)
I've wrote a java code and compiled it. (foo1.6.class)
According to my search, my local machine has Java 1.6, and the tomcat server that I uploaded foo1.6.class only accepts version number 1.5 This means that I have to have Java 1.5 to compile?
I believe it is the cause that bad version number error is thrown like below.
My question is, is there any way I can compile my Java file using 1.5 version number?
Looked at javac cmd but seems like it is not part of the option. But I don't think remove 1.6 and install Java 1.5 for this reason is not really good option neither. How do people deal with this kind of situation?
Thanks in advance!
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
javac -target 1.5 -source 1.5
。仅当您还使用不高于目标的源兼容性版本时,Java 编译器才允许您使用低于当前目标的版本号(源版本低于 1.4 时有例外)。
You can use
javac -target 1.5 -source 1.5
.The Java compiler allows you to use a lower than it's current target version number only if you also use a source compatibility version that is not higher than the target (there are execptions for source versions below 1.4).
注意 -source 和 -target 参数吗?
Note the -source and -target arguments?
在您的情况下,正确的答案是 x4u 指出的 -source 和 -target 标志。
您询问是否需要卸载 Java 6 才能安装 Java 5。这很少有必要。 “javac”编译器随 JDK 一起提供,并且可以安装许多这样的编译器。您可以考虑安装 Java 5 JDK,而不卸载 Java 6,然后仅使用该安装中的 javac 进行编译。这将确保您的代码将在 Java 5 运行时上运行(这也是我个人所做的)。
The right answer in your situation is the -source and -target flags as pointed out by x4u.
You ask about needing to uninstall Java 6 to be able to install Java 5. This is rarely necessary. The "javac" compiler comes with the JDK, and it is possible to have many of these installed. You may consider installing the Java 5 JDK without uninstalling Java 6, and then just compile with javac from that installation. This will ensure that your code will run with a Java 5 runtime (and is what I personally do).