简体   繁体   English

Windows 忽略 JAVA_HOME:如何将 JDK 设置为默认值?

[英]Windows ignores JAVA_HOME: how to set JDK as default?

How do I persuade Windows to use the JDK instead of a JRE?如何说服 Windows 使用 JDK 而不是 JRE?

This question has been asked before, here and elsewhere:之前,这里和其他地方已经提出过这个问题:

How do I set the default Java installation/runtime (Windows)? 如何设置默认 Java 安装/运行时 (Windows)?

The trouble is that Windows ignores JAVA_HOME and it also ignores the fact that I made the JDK bin directory the first entry in the path.问题是 Windows 忽略了JAVA_HOME并且它也忽略了我将 JDK bin 目录作为路径中的第一个条目这一事实。

When I run java -version from the command line, instead of invoking my JDK 1.6 installation, it runs the 1.7 JRE.当我从命令行运行java -version时,它不是调用我的 JDK 1.6 安装,而是运行 1.7 JRE。

My guess is that this is a problem specific to 1.7, and Windows 7 is doing something it shouldn't with the registry.我的猜测是这是 1.7 特有的问题,而 Windows 7 正在做一些它不应该对注册表进行的操作。

Any ideas on how to fix this?有想法该怎么解决这个吗?

Edit : Oops.编辑:哎呀。 I wrote "first entry in the classpath" above, when I meant "path".当我的意思是“路径”时,我在上面写了“类路径中的第一个条目”。 Sorry.对不起。

On Windows, the same java executable can load DLLs for different versions of Java.在 Windows 上,同一个java可执行文件可以为不同版本的 Java 加载 DLL。 It looks at the directory from which it's running to see if it contains libraries for a particular version of Java.它查看运行它的目录,看它是否包含特定 Java 版本的库。 If not, it uses the Windows Registry to locate the default version of Java for the system.如果没有,它会使用 Windows 注册表来定位系统的默认 Java 版本。

The Java installer will put a copy of java.exe (but no libraries) in the the Windows\system32 C:\Program Files (x86)\Common Files\Oracle\Java\javapath directory, and add that directory to the beginning of the PATH variable. Java 安装程序会将java.exe的副本(但没有库)放在Windows\system32 C:\Program Files (x86)\Common Files\Oracle\Java\javapath目录中,并将该目录添加到PATH变量。

If you don't use a full path, the copy of java.exe to run is found by using the PATH system variable.如果不使用完整路径,则使用PATH系统变量找到要运行的java.exe副本。 Since this directory doesn't contain the DLLs of a particular Java runtime version, one is located one by looking at the registry.由于此目录不包含特定 Java 运行时版本的 DLL,因此可以通过查看注册表找到一个。

So, you either need to modify the registry, or replace the javapath entry with the version of Java you want in your PATH system (not user) variable.因此,您要么需要修改注册表,要么将javapath条目替换为PATH系统(而非用户)变量中所需的 Java 版本。

In Windows 8, you may want to remove C:\ProgramData\Oracle\Java\javapath directory.在 Windows 8 中,您可能需要删除C:\ProgramData\Oracle\Java\javapath目录。

from path从路径

It solved my issue.它解决了我的问题。

I have this issue too.我也有这个问题。 I am running 1.6 but want to build the code I'm working on with 1.5.我正在运行 1.6,但想构建我正在使用 1.5 处理的代码。 I've changed the JAVA_HOME and PATH (both user and system) to no avail.我已更改JAVA_HOMEPATH (用户和系统)无济于事。

The answer is that the installer for 1.6 dropped java.exe , javaw.exe , and javaws.exe into my Windows\System32 folder (Windows 7).答案是 1.6 的安装程序将java.exejavaw.exejavaws.exe放入了我的Windows\System32文件夹(Windows 7)。

I solved it by renaming those files to java_wrong.exe , javaw_wrong.exe , and javaws_wrong.exe .我通过将这些文件重命名为java_wrong.exejavaw_wrong.exejavaws_wrong.exe解决了这个问题。 Only after doing that does it pick up the correct version of java as defined in JAVA_HOME and PATH .只有在这样做之后,它才会选择JAVA_HOMEPATH中定义的正确 java 版本。 I renamed the files thusly because that deleted them in an easily reversible manner.因此,我重命名了这些文件,因为这以一种易于恢复的方式删除了它们。

Hope this helps!希望这可以帮助!

Windows doesn't ignore anything. Windows 不会忽略任何东西。 This is an issue with your setup;这是您的设置问题; Windows just uses what you provide. Windows 只使用您提供的内容。 It has no special knowledge of JAVA_HOME .它没有JAVA_HOME的特殊知识。

CLASSPATH has nothing to do with Windows, either. CLASSPATH也与 Windows 无关。 To Windows it's only an environmental variable that gets expanded to a folder location.对于 Windows,它只是一个扩展到文件夹位置的环境变量。

Check your %PATH% environmental variable.检查您的%PATH%环境变量。 It's what's making Windows find one before the other.这就是让 Windows 先于其他找到的原因。 The path (as the post you linked to said) should point to %JAVA_HOME%\bin;<remainder of path> .路径(如您链接到的帖子所说)应指向%JAVA_HOME%\bin;<remainder of path> Again, the post you linked to gave you a way to set this using a batch file.同样,您链接到的帖子为您提供了一种使用批处理文件进行设置的方法。

(For others who might not know this: The easiest way to inspect the %PATH% is to open a command prompt and type echo %PATH% . You can also get there by right-clicking on Computer in the right pane of the Start Menu and choosing Properties , then Advanced System Settings , and the tne Environmental Variables button.) (对于可能不知道这一点的其他人:检查 %PATH% 的最简单方法是打开命令提示符并键入echo %PATH% 。您也可以通过右键单击“开始”菜单右侧窗格中的Computer来到达那里并选择Properties ,然后选择Advanced System Settings和 tne Environmental Variables按钮。)

I had the same issue.我遇到过同样的问题。 I have a bunch of Java versions installed and for some reason Java 1.7 was being used instead of Java 1.6, even though I specified in the path to use 1.6 (C:\jdk1.6.0_45_32\bin).我安装了一堆 Java 版本,出于某种原因,使用的是 Java 1.7 而不是 Java 1.6,即使我在路径中指定了使用 1.6 (C:\jdk1.6.0_45_32\bin)。

I had to move the path of the JDK I wanted to use (1.6) to be the first entry in the PATH environment variable to make sure Windows uses 1.6 instead of 1.7.我必须将要使用的 JDK 的路径 (1.6) 移动为 PATH 环境变量中的第一个条目,以确保 Windows 使用 1.6 而不是 1.7。

So, for example, the PATH environment variable before was:因此,例如,之前的 PATH 环境变量是:

C:\Program Files (x86);...<other entries>;C:\dev\ant181\bin;C:\jdk1.6.0_45_32\bin

and after I moved the jdk to be first, it worked:在我将 jdk 移到第一位之后,它起作用了:

C:\jdk1.6.0_45_32\bin;C:\Program Files (x86);...<other entries>;C:\dev\ant181\bin

I guess the Windows installer of Java 1.7 installed it to some other directory already in the PATH, thus getting used first instead of the specified custom PATH entry C:\jdk1.6.0_45_32\bin;我猜 Java 1.7 的 Windows 安装程序将它安装到 PATH 中已经存在的其他目录中,因此首先使用它而不是指定的自定义 PATH 条目 C:\jdk1.6.0_45_32\bin;

在此处输入图像描述 Suppose you have install JDK 10 after JDK 8 and in the system environment variable set path like "C:\ProgramData\Oracle\Java\javapath" then Java version control by this path.假设您在 JDK 8 之后安装了 JDK 10,并且在系统环境变量中设置了“C:\ProgramData\Oracle\Java\javapath”之类的路径,然后通过该路径进行 Java 版本控制。 it will ignore JAVA_HOME even jdk 1.8 path set here So remove "C:\ProgramData\Oracle\Java\javapath" in path to get effect of JAVA_HOME path它会忽略 JAVA_HOME 甚至 jdk 1.8 路径设置在这里所以删除路径中的“C:\ProgramData\Oracle\Java\javapath”以获得 JAVA_HOME 路径的效果

For my Case in 'Path' variable there was a parameter added like ' C:\ProgramData\Oracle\Java\javapath;对于我在'Path'变量中的案例,添加了一个参数,例如“ C:\ProgramData\Oracle\Java\javapath; '. '。 This location was having java.exe , javaw.exe and javaws.exe from java 8 which is newly installed via jdk.exe from Oracle.这个位置有来自 java 8 的java.exejavaw.exejavaws.exe ,它们是通过 Oracle 的jdk.exe新安装的。

I've removed this text from Path where my Path already having %JAVA_HOME%\bin with it.我已从路径中删除了此文本,其中我的路径已经具有%JAVA_HOME%\bin

Now, the variable 'JAVA_HOME' is controlling my Java version which is I wanted.现在,变量'JAVA_HOME'正在控制我想要的 Java 版本。

I had Java 7 and 8 installed and I want to redirect to java 7 but the java version in my cmd prompt window shows Java 8.我安装了 Java 7 和 8,我想重定向到 java 7,但我的 cmd 提示窗口中的 java 版本显示 Java 8。
Added Java 7 bin directory path ( C:\Program Files\Java\jdk1.7.0_10\bin ) to PATH variable at the end, but did not work out and shows Java 8. So I changed the Java 7 path to the starting of the path value and it worked.最后将 Java 7 bin 目录路径( C:\Program Files\Java\jdk1.7.0_10\bin )添加到 PATH 变量中,但没有成功并显示 Java 8。所以我将 Java 7 路径更改为开头路径值,它起作用了。
Opened a new cmd prompt window and checked my java version and now it shows Java 7打开一个新的 cmd 提示窗口并检查我的 java 版本,现在它显示 Java 7

将 Path 环境变量设置为所需的 jdk/bin 目录

In my case I had Java 7 and 8 (both x64) installed and I want to redirect to java 7 but everything is set to use Java 8. Java uses the PATH environment variable:在我的情况下,我安装了 Java 7 和 8(均为 x64),我想重定向到 java 7,但一切都设置为使用 Java 8。Java 使用 PATH 环境变量:

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

as the first option to look for its folder runtime (is a hidden folder).作为查找其文件夹运行时的第一个选项(是一个隐藏文件夹)。 This path contains 3 symlinks that can't be edited.此路径包含 3 个无法编辑的符号链接。

In my pc, the PATH environment variable looks like this:在我的电脑中,PATH 环境变量如下所示:

C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin; C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin;

In my case, It should look like this:就我而言,它应该如下所示:

C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin; C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin;

I had to cut and paste the symlinks to somewhere else so java can't find them, and I can restore them later.我不得不将符号链接剪切并粘贴到其他地方,因此 java 找不到它们,我可以稍后恢复它们。

After setting the JAVA_HOME and JRE_HOME environment variables to the desired java folders' runtimes (in my case it is Java 7), the command java -version should show your desired java runtime.在将 JAVA_HOME 和 JRE_HOME 环境变量设置为所需的 java 文件夹的运行时(在我的情况下是 Java 7)之后,命令java -version应该显示您所需的 java 运行时。 I remark there's no need to mess with the registry.我说没有必要弄乱注册表。

Tested on Win7 x64.在 Win7 x64 上测试。

This issue is probably because of the earlier versions of Java installed in your System.此问题可能是因为您的系统中安装了较早版本的 Java。 First check your Environment Variables Carefully and remove all the Environment Variables related to the previous versions of JAVA and replace those paths to首先仔细检查您的环境变量并删除与以前版本的JAVA相关的所有环境变量并将这些路径替换为

C:\Program Files\Java\<your new jdk version>\bin

There's an additional factor here;这里还有一个额外的因素; in addition to the java executables that the java installation puts wherever you ask it to put them, on windows, the java installer also puts copies of some of those executables in your windows system32 directory, so you will likely be using which every java executable was installed most recently.除了 java 安装程序放置在您要求放置它们的任何位置的 java 可执行文件之外,在 windows 上,java 安装程序还将其中一些可执行文件的副本放在您的 windows system32 目录中,因此您可能会使用每个 java 可执行文件是最近安装的。

以防万一您将 .BAT 文件用作 Windows 服务,我建议您卸载 Windows 服务并在更改 %JAVA_HOME% 以指向正确的 Java 版本后重新安装它。

After struggling with this issue for some time and researching about it, I finally managed to solve it following these steps:在为这个问题苦苦挣扎并研究了一段时间后,我终于设法按照以下步骤解决了它:

1) install jdk version 12 1)安装jdk版本12
2) Create new variable in systems variable 2)在系统变量中创建新变量
3) Name it as JAVA_HOME and give jdk installation path 3)命名为JAVA_HOME并给出jdk安装路径
4) add this variable in path and move it to top. 4)在路径中添加这个变量并将其移动到顶部。
5) go to C:\Program Files (86)\Common Files\Oracle\Java\javapath and replace java.exe and javaw.exe with the corresponding files with the same names from the pathtojavajdk/bin folder 5) 进入C:\Program Files (86)\Common Files\Oracle\Java\javapath,将java.exe和javaw.exe替换为pathtojavajdk/bin文件夹下对应的同名文件

Finally, I checked the default version of java in cmd with "java -version" and it worked!最后,我用“java -version”检查了cmd中java的默认版本,它工作了!

I had the same problem, in user environment variable settings I was having JAVA_HOME and PATH configured properly but it didn't work.我遇到了同样的问题,在用户环境变量设置中,我正确配置了 JAVA_HOME 和 PATH 但它不起作用。 As I update my system environment variables then it started to work.当我更新我的系统环境变量时,它开始工作。

I was facing the same issue.我面临着同样的问题。 I had a java version of 8.11.我有一个 8.11 的 java 版本。 I had these two in my path: C:\Program Files\Java\jdk1.8.0_51\bin and C:\ProgramData\Oracle\Java\javapath我的路径中有这两个: C:\Program Files\Java\jdk1.8.0_51\binC:\ProgramData\Oracle\Java\javapath

What I did is that I had changed the jdk to %JAVA_HOME%\bin like this and renamed the directory ( C:\ProgramData\Oracle\Java\javapath ) of javapath to javapath1 and it resolved my problem.我所做的是我像这样将jdk更改为%JAVA_HOME%\bin并将javapath的目录( C:\ProgramData\Oracle\Java\javapathjavapathjavapath1 ,它解决了我的问题。

Delete C:\ProgramData\Oracle\Java\javapath from Path in system variables & add your custom value for JAVA_HOME variable for User variables.从系统变量中的路径中删除 C:\ProgramData\Oracle\Java\javapath 并为用户变量添加自定义值 JAVA_HOME 变量。 This will work in Windows 10.这将在 Windows 10 中运行。

Folks who responded by stating setting PATH variable should work then please explain this:通过声明设置 PATH 变量来回应的人应该可以工作,然后请解释一下:

在此处输入图像描述

You need to remove the entry C:\Program Files (x86)\Common Files\Oracle\Java\javapath from PATH and make sure you have %JAVA_HOME%\bin entry under PATH and JAVA_HOME defined.您需要从 PATH 中删除条目 C:\Program Files (x86)\Common Files\Oracle\Java\javapath 并确保在 PATH 和 JAVA_HOME 下定义了 %JAVA_HOME%\bin 条目。

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

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