如何解决VScode的UnsupportedClassVersionError错误?

8

问题

当我运行我的 HelloWorld 程序时,它返回:

"java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0 "

如何解决这个问题?

截图

enter image description here

代码

public class HelloWorld{
    public static void main(String[] args) {
        System.out.println("123");
    }
}

你正在使用哪个命令进行编译和运行? - Jorn Vernee
3个回答

6
这个问题与Java Debugger扩展相关。我遇到了相同的问题,已经记录在问题日志中:https://github.com/Microsoft/vscode-java-debug/issues/555
问题只存在于像HelloWorld这样的单文件应用程序中。如果您运行Maven项目,则没有问题。
Medsonk的指示对我有用:https://github.com/Microsoft/vscode-java-debug/issues/555#issuecomment-478464496 总结: 1. 确保完全卸载jdk8 2. 安装jdk11 3. 在launch.json中添加“vmArgs”:“--enable-preview” 4. F1,“Java: Clean……”和“Java: Force……” 5. 再次运行独立文件

已经在扩展程序 Red Hat 的 Java(TM) 语言支持 中修复 链接 - mjuchi
1
刚刚安装了最新的VSCode和Java扩展,遇到了同样的问题;在launch.json > configuration中添加"vmArgs": "--enable-preview",问题得以解决。 - Beygi

0

-2

我的情况是在Ubuntu上Java和Javac之间存在版本冲突;
只需运行此代码:

sudo update-alternatives --config javac

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接