Java Tomcat-不受支持的类异常

Java Tomcat-不受支持的类异常,java,tomcat,version,Java,Tomcat,Version,我在Tomcat web应用程序中部署编译类时遇到问题:我正在部署一个要从servlet调用的类,但是,当我运行应用程序时,它无法告诉我由于.class文件中的不受支持的ClassVersionError:错误版本号而导致的ServletException:分配servlet实例时出错 据经理报告,Tomcat正在使用Java 1.5.0_06。我的类是使用Java1.6.0_14编译的。在已经存在的任何类上运行javap都会告诉我“主要版本46,次要版本0”,它的初始版本应该是1.2.0,并且

我在Tomcat web应用程序中部署编译类时遇到问题:我正在部署一个要从servlet调用的类,但是,当我运行应用程序时,它无法告诉我由于.class文件中的
不受支持的ClassVersionError:错误版本号而导致的
ServletException:分配servlet实例时出错

据经理报告,Tomcat正在使用Java 1.5.0_06。我的类是使用Java1.6.0_14编译的。在已经存在的任何类上运行javap都会告诉我“主要版本46,次要版本0”,它的初始版本应该是1.2.0,并且不再可供下载。我能找到的最古老的版本是1.2.1_004,它甚至没有编译


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

这很简单:您使用比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代码

我是否需要将Java版本与Tomcat环境或已有的类相匹配


您需要确保您的代码是使用您正在使用的JVM支持的版本(小于或等于)编译的;但是不需要,这不需要与Tomcat代码库的构建版本相同-这两个代码库可以是彼此独立的jvm版本,只要它们都受您使用的jvm支持。

您可以将Tomcat使用的JDK更新为1.6,而不是重新编译源代码使其与1.5兼容


您应该能够通过将
JAVA_HOME
环境变量设置为指向JAVA 1.6安装来更改此设置。

我不久前也遇到了同样的UnsupportedClassVersionError问题。根本原因不是我自己编译的代码,而是需要一些库,这些库是用较新的JDK编译的。

就像他回答了自己的问题一样!我不知道你的情况,但我需要所有我能得到的帮助:)我很高兴终于遇到了一个我实际上有能力回答的问题。太棒了,谢谢!我马上就去试试。是的,我也需要所有能得到的帮助:我很想把所有的东西都更新到更新更好的版本,但我没有这样做的权限/如果您使用相同版本的Java进行开发和生产,您的生活将变得更加轻松。