VSCode中如何修复Java文件版本和编译版本不同

时间:2019-03-31 09:34:59

标签: java visual-studio-code

当我使用VSCode运行Java程序时,它总是显示一个错误,因为类文件的版本而导致无法识别已编译的类文件。

我已经检查了Mac的环境。 javac -versionjava -version显示javac 1.8.0_181java 1.8.0_181

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

如果我运行代码,它将显示错误:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Hello has been compiled by a more recent version of the Java Runtime (class file version 56.65535), this version of the Java Runtime only recognizes class file versions up to 52.0

1 个答案:

答案 0 :(得分:0)

  1. 卸载jdk8 clean(Link
  2. 安装jdk11(Link
  3. 在launch.json中添加“ vmArgs”:“-enable-preview”
  4. F1,“ Java:清理Java语言服务器工作区”和“ Java:强制Java编译”
  5. 再次运行独立文件