简体   繁体   English

无法在 Windows 中更改 Java 版本

[英]Unable to change Java Version in windows

Previously I am using Java 1.8 in my machine.以前我在我的机器上使用Java 1.8 But now i need to use Java 1.6 .但现在我需要使用Java 1.6 So I changed the below values in system environment variables .所以我在系统环境变量中更改了以下值。

JAVA_HOME JAVA_主页

U:\POC\jdk1.6.0_31 U:\POC\jdk1.6.0_31

PATH小路

U:\POC\jdk1.6.0_31\bin\ or %JAVA_HOME%/bin U:\POC\jdk1.6.0_31\bin\%JAVA_HOME%/bin

Also I restarted my system我也重新启动了我的系统

Even after that it is displaying java version as 1.8 in my command prompt.即使在那之后它在我的命令提示符中显示 java 版本为 1.8。

命令提示符

Can anyone advice on how to resolve this issue?谁能建议如何解决这个问题?

when latest version is installed in system it automatically set javapath in environment variable like "C:\\ProgramData\\Oracle\\java\\javapath".在系统中安装最新版本时,它会自动在环境变量中设置 javapath,如“C:\\ProgramData\\Oracle\\java\\javapath”。 Remove this from environment variables.从环境变量中删除它。 在此处输入图片说明

This suggestion may seem obvious, but I urge you to carefully consider this advice.这个建议似乎很明显,但我敦促你仔细考虑这个建议。

I have encountered this issue before, and frequently, it has been due to an overlooked entry within the long list of items making up the Windows Path.我以前遇到过这个问题,而且经常是由于在构成 Windows 路径的一长串项目中忽略了一个条目。 It is often an path entry where it is not immediately obvious that any of the java executables (javaw.exe, javaw.exe or javaws.exe) may reside.它通常是一个路径条目,其中任何 java 可执行文件(javaw.exe、javaw.exe 或 javaws.exe)可能驻留都不是很明显。 A common culprit is C:\\ProgramData\\Oracle\\Java\\javapath一个常见的罪魁祸首是C:\\ProgramData\\Oracle\\Java\\javapath

You will therefore need to push upwards, your desired java/bin path above these.因此,您需要向上推,您想要的java/bin路径高于这些。 If in doubt, push it right to the top!如果有疑问,请将其推到顶部!

In the unlikely event that this does not work, then try making changes from within the java admin console (Control Panel --> Java --> Java tab --> View Button).万一这不起作用,请尝试从 Java 管理控制台(控制面板 --> Java --> Java 选项卡 --> 查看按钮)内进行更改。

I've not used Windows in a long time but I remember that there are 2 sections of environment variables.我很长时间没有使用 Windows,但我记得有 2 个环境变量部分。 One system scope, and the other user scope.一个系统范围,另一个用户范围。

  1. Check that nothing in the system scope is defining a JAVA_HOME .检查系统范围内是否没有定义JAVA_HOME
  2. Like @Stephen C said, post the output of echo %PATH% and echo %JAVA_HOME% .就像@Stephen C 说的那样,发布echo %PATH%echo %JAVA_HOME%
  3. Check that nothing comes before %JAVA_HOME%/bin in the path that may contain the java executable.检查可能包含java可执行文件的路径中%JAVA_HOME%/bin之前是否没有任何内容。 Things like Oracle client come with Java.诸如 Oracle 客户端之类的东西随 Java 一起提供。 You can always put %JAVA_HOME%/bin in front of the path instead of at the end.您始终可以将%JAVA_HOME%/bin放在路径前面而不是末尾。

@David Santiago's answer is a bad idea. @David Santiago 的回答是个坏主意。 If you let the IDE do your job, you won't be able to troubleshoot when there is a problem.如果让 IDE 来完成您的工作,您将无法在出现问题时进行故障排除。

The system displays the latest version installed on the system.系统显示系统上安装的最新版本。 That's informative only, that should not worry you.这只是提供信息,您不必担心。

If you are going to use version 6.0_31 of the Java JDK you only have to configure that in your IDE (Eclipse, Netbeans, IntelliJ IDEA, whatever) to compile using that version.如果您打算使用 Java JDK 的 6.0_31 版本,您只需在您的 IDE(Eclipse、Netbeans、IntelliJ IDEA 等)中配置它,即可使用该版本进行编译。

Example例子

I recently ran into this as I have added in JAVA 9, 11, and 17 along with my previous JAVA 6, and 8 versions.我最近遇到了这个问题,因为我添加了 JAVA 9、11 和 17 以及我之前的 JAVA 6 和 8 版本。
I incorrectly assumed that env variables hadn't changed with the newer releases, but I was obviously wrong.我错误地认为 env 变量没有随着新版本的变化而改变,但我显然错了。

When I ran java -version it returned JAVA 9 and javac -version returned JAVA 17. So, I incorrectly tried to add a %JAVA_HOME% variable, as I had always done in pre-JAVA 8 versions under the Windows Environment Settings. When I ran java -version it returned JAVA 9 and javac -version returned JAVA 17. So, I incorrectly tried to add a %JAVA_HOME% variable, as I had always done in pre-JAVA 8 versions under the Windows Environment Settings. However, despite moving that addition to the front of the PATH, it didn't change anything!然而,尽管把那个附加物移到了 PATH 的前面,它并没有改变任何东西!

This article explains the issue: Java SE 8 Update 171 Uses Different Windows Path (Doc ID 2412304.1) , however it doesn't give any solutions.这篇文章解释了这个问题: Java SE 8 Update 171 Uses Different Windows Path (Doc ID 2412304.1) ,但是它没有给出任何解决方案。

The path to this Windows folder is:此 Windows 文件夹的路径是:

C:\ProgramData\Oracle\Java\javapath C:\ProgramData\Oracle\Java\javapath

Notice is under a ProgramData folder, not Program Files.注意位于 ProgramData 文件夹下,而不是 Program Files 下。 Moreover, it has been prepended to PATH under the Windows env settings.此外,它已在 Windows 环境设置下添加到 PATH 中。 This folder is where java has 3 symbolic links set that will override the PATH changes you make and sure enough it was pointing to the JAVA versions I returned from -version on java and javac.这个文件夹是 java 设置了 3 个符号链接的位置,这些链接将覆盖您所做的 PATH 更改,并且确定它指向 JAVA 版本,我从 Z93F725A07423FE21C889F448B36 上的 -version 返回。 Even if I tried to set a JAVA_HOME as the first entry in path, it still didn't work!即使我尝试将 JAVA_HOME 设置为路径中的第一个条目,它仍然不起作用!

I found two solutions here at: JDK 8 and C:\ProgramData\Oracle\Java\javapath我在这里找到了两个解决方案:JDK 8 和 C:\ProgramData\Oracle\Java\javapath

How to switch JDK version in cmd more flexibly You can either prepend your own directory to the PATH to overwrite the Oracle one, or follow the convention to append to the end of PATH variable like always did.如何更灵活地在 cmd 中切换 JDK 版本您可以将自己的目录添加到 PATH 以覆盖 Oracle 之一,或者按照约定将 Z9516DFB15F51C7EE19A4D46B8C0DBE1D I prefer the second one, here is how to do it.我更喜欢第二个,这里是如何做到的。

The second solution uses two batch files with a env variable JAVA_HOME setting similar to what I had always used.第二种解决方案使用两个批处理文件,其环境变量 JAVA_HOME 设置类似于我一直使用的设置。

On command prompt use these commands to set envirnment variables在命令提示符下使用这些命令设置环境变量

JAVA_HOME=C:\Program Files\Java\jdk-11.0.17 JAVA_HOME=C:\Program Files\Java\jdk-11.0.17

set PATH=%JAVA_HOME%\bin;%PATH%设置 PATH=%JAVA_HOME%\bin;%PATH%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM