Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly installing java 8 along with java 7

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?

like image 214
bachr Avatar asked Oct 12 '14 11:10

bachr


People also ask

Can I have both Java 7 and 8 installed?

You can install any number of JDK instances on your computer (and use them in projects or tools), but only one can be used as the main with JAVA_HOME env variable and its bin dir should be added to the PATH value like %JAVA_HOME%\bin .

Can I install Java 8 and Java 11 together?

I know you can install both. Java 8 and Java 11 and keep them at the same time...

Can we have two Java versions installed?

It is very possible to run multiple versions of Java on the same machine so you can run your existing applications and Ignition at the same time.

Is JDK 8 backwards compatible?

Backward Compatibility Java versions are expected to be binary backwards-compatible. For example, JDK 8 can run code compiled by JDK 7 or JDK 6. It is common to see applications leverage this backwards compatibility by using components built by different Java version.


1 Answers

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 
like image 79
Chris Avatar answered Oct 10 '22 17:10

Chris