更改 Windows 10 的 Java 版本?

huangapple go评论82阅读模式
英文:

Windows 10 change java version?

问题

我正在使用Intellij IDEA + JDK 14并且想要将我的项目作为JAR在控制台中执行。为此,我使用以下命令:

java -jar pathToJar

总是出现以下错误信息:

Exception in thread "main" java.lang.UnsupportedClassVersionError: de/marco/api/Main has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

我知道这个错误信息的含义,但不知道如何修复。我已经在系统的“PATH”下设置了指向我的JDK路径。我的计算机正在使用Java 8

C:\Users\User>java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)

有人能帮我解决如何更改计算机的Java版本,以便我可以为编译器使用更新的版本吗?非常感谢。

英文:

Im working with Intellij IDEA + JDK 14 and want to execute my project as jar in my console. Herefor I use

java -jar pathToJar

There is always the following Error message :

Exception in thread "main" java.lang.UnsupportedClassVersionError: de/marco/api/Main has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

I know what this error message means, but I dont know how to fix. I´ve set already in my system under "PATH" the path to my JDK. My computer uses Java 8 :

C:\Users\User>java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)

Could anyone help me how to change the Java version of my Computer, that i could use a newer version for my compiler. Thanks a lot

答案1

得分: 1

很简单:

  • 要么告诉IntelliJ将你的类编译为Java 8字节码
  • 要么确保你的Windows命令行指向你的Java 11设置

对于IntelliJ:你进入“项目设置”,然后转到“SDKs”(在平台设置下)。你可以将其指向JDK8的安装位置。

或者,由于你可能有意安装了Java 14:找到它在磁盘上的位置,并确保你的Windows PATH变量指向该位置。或者只需在你的PATH某处放置一些“包装”脚本,并让该包装脚本调用“Java 14”的Java。

英文:

It is very simple:

  • either you tell IntelliJ to compile your classes to java 8 bytecode
  • or you ensure that your windows command line is pointing to your java 11 setup

For Intellij; you look into "Project Settings", then turn to "SDKs" under Platform Settings. You could point that to a JDK8 installation.

Alternatively, as you probably installed Java 14 on purpose: locate where it sits on your disk, and ensure your Windows PATH variable points to that location. Or just put some "wrapper" script somewhere into your PATH, and have that wrapper script call the "java 14" java.

答案2

得分: 0

你编写了程序并使用比之前执行程序时更高版本的JDK进行了编译。
只需转到构建设置,并将编译器兼容性级别设置为你正在使用的JDK版本。
如果你想使用JDK 14而不是8,只需卸载JDK 8,Java 将使用 JDK 14。希望这有帮助 更改 Windows 10 的 Java 版本?

英文:

you wrote the program and compiled it using a higher version of jdk than you used to execute it.
Just go to build settings and set the compiler compliance level to the jdk you are using.
If you want to use JDK 14 instead of 8, just uninstall jdk 8 and java will use jdk14. I hope this helped 更改 Windows 10 的 Java 版本?

答案3

得分: 0

你的程序无法运行,因为你的计算机指向了 PATH 中的旧版本 Java JDK。你可以卸载 Java 8,然后重新安装 Java 14。在某些情况下,如果你不想删除旧版本,或者你已经手动更改了 PATH 变量,那么:

步骤一:从 PATH 中删除旧版本的 Java JDK 项目。

在开始按钮旁边的搜索栏中键入“this pc” -> 右键单击“此电脑”,选择“属性” -> 点击左侧边栏中的“高级系统设置” -> 点击“高级”选项卡 -> 点击底部的“环境变量”按钮 -> 修改“系统变量”面板下的“Path”变量,删除所有旧的 Java 路径信息(或在其他变量中具有旧的 Java 信息的任何项目)。如果操作正确,现在应该无法运行 java -version。

步骤二:从安装程序安装 Java JDK 14

新的 Java 路径信息将自动添加到 PATH 环境变量中。你不需要手动更新任何内容。(如果你确实想要更新,可以按照步骤一更改 PATH)。

英文:

You program didn't work because your computer points to an old version of java JDK in PATH. You can just uninstall Java 8 and reinstall java 14. In cases, if don't want to delete the old one or you have manually changed the PATH variable, then:

Step 1: Delete old items of Java JDK from PATH.

Type "this pc" in the search bar next to the start button -> Right click "This PC", choose "Properties" -> click "Advanced system settings" on the left side bar -> click "Advanced" tab -> click "Environment Variables" button at bottom -> Modify the "Path" variable under System variables panel and DELETE all your old java path information(or any item in other variables with your old java information). If you done right, java -version should not work right now.

Step 2: install Java JDK 14 from installer

New Java PATH info will be automatically added to the PATH environment variable. You don't need to manually update anything. (If you do want, follow step 1 to change PATH)

huangapple
  • 本文由 发表于 2020年8月18日 18:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定