gpt4 book ai didi

java - Tomcat - UnsupportedClassException

转载 作者:行者123 更新时间:2023-11-30 07:38:24 26 4
gpt4 key购买 nike

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

据经理报告,Tomcat 使用的是 Java 1.5.0_06。我的类(class)是使用 Java 1.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 - UnsupportedClassException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com