Properly installing java 8 along with java 7

蹲街弑〆低调 提交于 2019-12-17 15:27:08

问题


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

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 PATH variable points to the older version (i.e. 1.7).

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


回答1:


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

  • \windows\system32 contains Java 8 java.exe, javaw.exe and javaws.exe. Your path probably has system32 near 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.exe and javaws.exe as symlinks to the JRE 8 executables.

I've deleted the system32 files and removed C:\programdata\Oracle\Java\javapath from the system path. This seems to cure the problem. I can now switch versions by pointing JAVA_HOME and PATH to the appropriate folders.

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.

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.

@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



回答2:


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

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

Click "SOFTWARE" within the "HKEY_LOCAL_MACHINE" registries

Click "JavaSoft" within the "SOFTWARE" registries

Click "Java Runtime Environment" within the "JavaSoft" list of registries here you can see different versions of installed 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"

Now the magic has been completed




回答3:


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.




回答4:


I had the same problem then realized my program was running out of c:\Windows\SysWOW64 and 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.




回答5:


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

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

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



回答6:


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




回答7:


Thanks @Chris, for me it was solved just deleting the files from 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.

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




回答8:


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

I had Java 7 and Java 8 in the system.

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

It worked for me :)




回答9:


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

As a partial answer (since this might not be intended as 'properly' installed) I'm doing quite fine using Cygwin to 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 1 and adjust your local paths accordingly):

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

(mvn --version output)

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"

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

(mvn --version output)

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"

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




回答10:


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

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


来源:https://stackoverflow.com/questions/26324486/properly-installing-java-8-along-with-java-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!