正确安装 java 8 和 java 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26324486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:14:13  来源:igfitidea点击:

Properly installing java 8 along with java 7

javajava-7java-8

提问by bachr

I've JDK 1.7 installed on my windows 7 machine and after installing JDK 1.8 u20 I'm having following error:

我在 Windows 7 机器上安装了 JDK 1.7,安装 JDK 1.8 u20 后出现以下错误:

C:\>java -version
Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
has value '1.8', but '1.7' is required.
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.

My PATHvariable points to the older version (i.e. 1.7).

我的PATH变量指向旧版本(即 1.7)。

What is wrong here and how I could use java 8 along with java 7?

这里有什么问题以及我如何将 java 8 与 java 7 一起使用?

采纳答案by Chris

The problem is that Java 8 installs a lot of stuff that you don't really need:

问题是 Java 8 安装了很多你并不真正需要的东西:

  • \windows\system32contains Java 8 java.exe, javaw.exeand javaws.exe. Your path probably has system32near the beginning, so these tend to be run by default.

  • The system path variable starts with C:\programdata\Oracle\Java\javapath. This folder contains java.exe, javaw.exeand javaws.exeas symlinks to the JRE 8 executables.

  • \windows\system32包含 Java 8 java.exe、javaw.exejavaws.exe。您的路径可能在开头附近有system32,所以这些往往是默认运行的。

  • 系统路径变量以C:\programdata\Oracle\Java\javapath 开头。该文件夹包含java.exejavaw.exejavaws.exe作为 JRE 8 可执行文件的符号链接。

I've deleted the system32files and removed C:\programdata\Oracle\Java\javapathfrom the system path. This seems to cure the problem. I can now switch versions by pointing JAVA_HOMEand PATHto the appropriate folders.

我已经删除了system32文件并从系统路径中删除了C:\programdata\Oracle\Java\javapath。这似乎可以解决问题。我现在可以通过将JAVA_HOMEPATH指向适当的文件夹来切换版本。

Oracle, it seems, are determined to make it hard to run multiple versions. This is understandable with the JRE, but it's crazy with JDKs, as developers almost always need multiple versions of Java.

甲骨文似乎决心让运行多个版本变得困难。这对 JRE 来说是可以理解的,但对 JDK 来说就很疯狂,因为开发人员几乎总是需要多个版本的 Java。

EDIT: I find this batch script is useful for switching JDKs. Usage: jdk.bat 6|7|8. You might have to edit the installation location for Java.

编辑:我发现这个批处理脚本对于切换 JDK 很有用。用法:jdk.bat 6|7|8。您可能需要编辑 Java 的安装位置。

@echo off
if "%1"=="" goto report
set _version=%1
shift
if "%1"=="DBG" shift & echo on
set _command=%1 %2 %3 %4 %5

set _jdkdir=
set _jdkver=
for /D %%f in ("C:\Program Files\java\"jdk1.%_version%.*) do call :found "%%f"
if "%_jdkdir%"=="" goto notfound

set java_home=C:\Program Files\java\%_jdkdir%
call :javapath
path %new_path%
goto :report

:javapath
    setlocal enabledelayedexpansion
    set _jdirs=
    for /D %%j in ("C:\Program Files\java\*") do set _jdirs=!_jdirs!#%%~fj\bin
    set _jdirs=%_jdirs%#

    set _javabin=%java_home%\bin
    set _fpath="%PATH:;=" "%"
    call :checkpath %_fpath%
    endlocal & set new_path=%_javabin%
goto :eof

:checkpath
    if _%1==_ goto :eof
    echo %_jdirs% | find /i "#%~1#" 1>nul 2>&1
    set _err=%errorlevel%
    if not %_err%==0 set _javabin=%_javabin%;%~1
    if %_err%==0 echo Removed %~1 from path
    shift
    goto :checkpath

:report
javac -version
%_command%
goto :eof

:notfound
echo No JDK matching [C:\Program Files\java\jdk1.%_version%.*] found.
goto :eof

:found
set _jdkdir=%~n1%~x1
for /F "tokens=2,3 delims=." %%a in ("%_jdkdir%") do set _jdkver=1.%%a.%%b
goto :eof

回答by RehanZahoor

You can't have your cake and eat it too. :) When you set your PATH variable to JDK 1.8, The problem should resolve. You can run your programs in JDK 1.7 by setting PATH manually using set PATH from command prompt or can go to the JDK 1.7 directory and run your program from there. But there can be only one JDK in your PATH.

你不能吃你的蛋糕也吃它。:) 当您将 PATH 变量设置为 JDK 1.8 时,问题应该会解决。您可以通过从命令提示符使用 set PATH 手动设置 PATH 来在 JDK 1.7 中运行您的程序,也可以转到 JDK 1.7 目录并从那里运行您的程序。但是您的 PATH 中只能有一个 JDK。

回答by Dici

You could define a java7alias that would lead to the binfolder of your JDK7, and then change your environment variable so that it points JDK8. The default JDK would then be JDK8.

您可以定义一个java7指向binJDK7 文件夹的别名,然后更改您的环境变量,使其指向 JDK8。默认的 JDK 将是 JDK8。

回答by Mycroft_IV

I had the same problem then realized my program was running out of c:\Windows\SysWOW64and hence running the old java.exe. Once I stopped running out of that directory (which contains the Java 7 exe), the problem went away since it followed the path properly to java8.

我遇到了同样的问题,然后意识到我的程序用完了c:\Windows\SysWOW64,因此运行旧的java.exe. 一旦我停止用完该目录(其中包含Java 7 exe),问题就消失了,因为它正确地遵循了到java8.

回答by satender

In the START menu type "regedit" to open the Registry editor

在开始菜单中输入“regedit”打开注册表编辑器

Go to "HKEY_LOCAL_MACHINE" on the left-hand side registry explorer/tree menu

转到左侧注册表资源管理器/树菜单上的“HKEY_LOCAL_MACHINE”

Click "SOFTWARE" within the "HKEY_LOCAL_MACHINE" registries

单击“HKEY_LOCAL_MACHINE”注册表中的“SOFTWARE”

Click "JavaSoft" within the "SOFTWARE" registries

单击“软件”注册表中的“JavaSoft”

Click "Java Runtime Environment" within the "JavaSoft" list of registries here you can see different versions of installed java

单击“JavaSoft”注册表列表中的“Java 运行时环境”,您可以看到不同版本的已安装 Java

Click "Java Runtime Environment"- On right hand side you will get 4-5 rows . Please select "CurrentVersion" and right Click( select modify option) Change version to "1.7"

单击“Java 运行时环境”- 在右侧,您将获得 4-5 行。请选择“当前版本”并右键单击(选择修改选项)将版本更改为“1.7”

Now the magic has been completed

现在魔法已经完成

回答by Krzysztof Ziomek

If you are not comfortable to remove any files in Windows manually, just put your JAVA_HOME path in the front of Windows dirs.

如果您不习惯手动删除 Windows 中的任何文件,只需将您的 JAVA_HOME 路径放在 Windows 目录的前面。

Define JAVA_HOME environment variable in Windows 7 and use it in variable PATH on first position of Path variable.

在 Windows 7 中定义 JAVA_HOME 环境变量,并在 Path 变量的第一个位置的变量 PATH 中使用它。

JAVA_HOME -> D:\dev\Java\jdk1.8.0_45
Path -> %JAVA_HOME%\bin;%SystemRoot%\system32;

回答by Alex MM

Thanks @Chris, for me it was solved just deleting the files from C:\Windows\System32.

谢谢@Chris,对我来说,只需从C:\Windows\System32.

I got this problem when using the Sencha Command, but of course it has nothing to do with Sencha. It is a Java installation issue.

我在使用 Sencha 命令时遇到了这个问题,当然它与 Sencha 无关。这是Java安装问题。

Apparantely this issue was also really annoying for people with Java 1.6 and 1.7 according to this post: Registry Key '...' has value '1.7', but '1.6' is required. Java 1.7 is Installed and the Registry is Pointing to it

显然,根据这篇文章,对于使用 Java 1.6 和 1.7 的人来说,这个问题也很烦人: 注册表项“...”的值为“1.7”,但需要“1.6”。Java 1.7 已安装并且注册表指向它

回答by Diego1974

This issue is annoying when dealing with multiple JDKs for developingon Windows 10 (I couldn't make use of the OS system path change as suggested here).

在处理多个 JDK在 Windows 10 上进行开发时,这个问题很烦人(我无法按照此处的建议使用操作系统系统路径更改)。

As a partial answer (since this might not be intended as 'properly' installed) I'm doing quite fine using Cygwinto switch JAVA_HOME and run Maven builds with different JDKs (1.7,1.8) installed (via Oracle installers). So if you have Cygwin installed (or can install and use it) and willing to use JAVA_HOME env variable (useful with Maven as in this example below) you could do like this (BUT be aware of the 'trick' in the answer at link 1and adjust your local paths accordingly):

作为部分答案(因为这可能不是“正确”安装的),我使用Cygwin切换 JAVA_HOME 并运行安装了不同 JDK(1.7、1.8)(通过 Oracle 安装程序)的 Maven 构建做得很好。因此,如果您安装了 Cygwin(或可以安装和使用它)并愿意使用 JAVA_HOME 环境变量(在下面的示例中对 Maven 有用),您可以这样做(但请注意链接中答案中的“技巧” 1并相应地调整您的本地路径):

export JAVA_HOME=/cygdrive/c/Progra~1/Java/jdk1.8.0_74

(mvn --version output)

(mvn --version 输出)

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00) Maven home: C:\Work2\bin\apache-maven-3.3.9-bin\apache-maven-3.3.9 Java version: 1.8.0_74, vendor: Oracle Corporation Java home: C:\Progra~1\Java\jdk1.8.0_74\jre Default locale: it_IT, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

阿帕奇Maven的3.3.9(bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47 + 01:00)的Maven家:C:\ WORK2 \ BIN \ Apache的Maven的3.3.9斌\ Apache的Maven的3.3.9爪哇版本:1.8.0_74,供应商:Oracle Corporation Java 主页:C:\Progra~1\Java\jdk1.8.0_74\jre 默认语言环境:it_IT,平台编码:Cp1252 操作系统名称:“windows 10”,版本:“10.0” ,拱:“amd64”,家庭:“dos”

export JAVA_HOME=/cygdrive/c/Work2/bin/Java/jdk1.7.0_79_64bit/

(mvn --version output)

(mvn --version 输出)

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00) Maven home: C:\Work2\bin\apache-maven-3.3.9-bin\apache-maven-3.3.9 Java version: 1.7.0_79, vendor: Oracle Corporation Java home: C:\Work2\bin\Java\jdk1.7.0_79_64bit\jre Default locale: it_IT, platform encoding: Cp1252 OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"

阿帕奇Maven的3.3.9(bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47 + 01:00)的Maven家:C:\ WORK2 \ BIN \ Apache的Maven的3.3.9斌\ Apache的Maven的3.3.9爪哇版本:1.7.0_79,供应商:Oracle Corporation Java 主页:C:\Work2\bin\Java\jdk1.7.0_79_64bit\jre 默认语言环境:it_IT,平台编码:Cp1252 操作系统名称:“windows 8.1”,版本:“6.3” ,拱门:“amd64”,家庭:“窗户”

https://stackoverflow.com/questions/14567191/export-java-home-with-spaces-in-cygwin

https://stackoverflow.com/questions/14567191/export-java-home-with-spaces-in-cygwin

回答by Punith Kumar

In my case, I uninstalled other versions of Java keeping Newer version

就我而言,我卸载了其他版本的 Java,保留了较新的版本

I had Java 7 and Java 8 in the system.

我的系统中有 Java 7 和 Java 8。

I Uninstalled 7 and kept 8, in the path, I already had Java 8.

我卸载了 7 并保留了 8,在路径中,我已经有了 Java 8。

It worked for me :)

它对我有用:)

回答by tito76

Edit this text in base your own values, save as ".reg", execute, enjoy :)

根据您自己的值编辑此文本,另存为“.reg”,执行,享受:)

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment.8.0_74]
"JavaHome"="C:\Program Files\Java\jre8"