小编典典

Tomcat-UnsupportedClassException

tomcat

我在Tomcat
Web应用程序中部署已编译的类时遇到问题:我正在部署一个要从Servlet调用的类,但是当我运行该应用程序时,它ServletException: Error allocating the servlet instance由于提示失败而无法告诉我UnsupportedClassVersionError: Bad version number in .class file

Tomcat正在使用管理器报告的Java 1.5.0_06。我的课程是使用Java
1.6.0_14编译的。在已经存在的任何类上运行javap会告诉我“主要版本46,次要版本0”应该是1.2.0初始版本,并且不再可供下载。我能找到的最旧的文件是1.2.1_004,甚至没有编译。

我是否需要将Java版本与Tomcat环境或已有的类相匹配?尽管我很乐意,但是使用更现代的Java重新编译整个项目目前对我来说并不可行。


阅读 333

收藏
2020-06-16

共1个答案

小编典典

这很容易:您使用比Tomcat下面的Java运行时更高的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编译器,只需向其抛出这些选项,即可使其生成Tomcat能够处理的1.5代码。

2020-06-16