Default Java on Windows machine - java

I have a Windows 10 x64 machine. I had Open JDK 10. My JAVA_HOME points to Open JDK 10 location. My Path variable has %JAVA_HOME%/bin; part.
Then I installed Java 8.
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Java installer changes Path.
C:\Users\user>set Path
Path=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;OTHER_STUFF_WITHOUT_JAVA
Now in command line java -version points to Java 8.
C:\Users\user>java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
But JAVA_HOME is still pointing to Open JDK 10.
C:\Users\user>set java_home
JAVA_HOME=C:\Java\jdk\jdk-10.0.2
My goal is to have Open JDK-10 as default Java.
This is not a duplicate
Default Java Path on Windows Machines
because there is no answer for me.

(Post comment as the answer for readability)
The easiest way to have several JDK and be sure to point towards the good one, is to avoid using the java installers. You should just unzip the JDKs in a folder and then modify your JAVA_HOME and PATH accordingly. You could also have several JAVA_HOME like JAVA_HOME_8 and JAVA_HOME_10 pointing to their respective JDK folder and make your JAVA_HOME=%JAVA_HOME_10% so you can change easily.

The easiest way i just found out is that the java 8 installer creates a new environment PATH entry:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
This will point to the latest installed JRE or JDK(i.e. the last in JDK or JRE) using the installer method.
So if your JAVA_HOME points to the JDK 10 OR JRE 10 just go to the Environment Variables and remove:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
From the PATH variable.
Then the values you used for JAVA_HOME will become effective.
This worked for me after installing java 8 and i had installed java 12

Related

How to know the jdk version on my machine?

I have recently uninstalled JDK 11 and installed JDK 8. For confirmation, I want to check which JDK is installed on my Windows 10 machine. I typed java -version on cmd then get the error message
java is not recognized as an internal or external command
How to know which JDK version installed on my PC?
you might need to add path in environment variables which you can find in Control Panel
open the Jdk where you installed and add until /bin in the path in environment variables.
Add until /bin in path variable in System Variables which is residing in Environment Variables.
Then do
java -version
which might show up.
If still problem persists, try restarting your pc and see.
You need to update your Windows path to include your %JAVA_HOME%\bin directory. %JAVA_HOME% is the directory that you installed Java into and is also an environment variable that you need to configure for command line execution of your applications. You can edit both of these in the Windows control panel and you should restart.
When you run java -version you will see the internal version number. This is explained here: https://en.wikipedia.org/wiki/Java_version_history.
Basically, you can ignore the 1. when reading version number. The _xxx is a reference to the most recent patch or build release.
On Windows 10, this required mapping the environment variable for JAVA_HOME to the JDK installation directory. Use these steps:
Run the installer for the JDK. (available for windows here: https://www.oracle.com/java/technologies/downloads/#jdk17-windows)
windows key -> Environment Variables, select the only result
In the System Properties window that opened, select Environment Variables
Select new button under the User variables section
Variable name: JAVA_HOME, Variable Value: <The JDK filepath from step 0>
ok all open menus
Close any open cmd prompt windows
open a new cmd window and type echo %JAVA_HOME% It should print the installation path for the JDK.
To get your jdk location in Windows, run this at a command prompt:
where java
This lists any and all locations of java.exe, including from your JAVA_HOME. For example, the 3rd line here reflects my JAVA_HOME location, where I'm pointing to JDK 8:
C:\Users\me> where java
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk1.8.0_202\bin\java.exe
Note for comparison that java -version does not reflect my JAVA_HOME location and in fact shows java version 11 instead of 8:
C:\Users\me> java -version
java version "11.0.15" 2022-04-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.15+8-LTS-149)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.15+8-LTS-149, mixed mode)
This is confusing because my Java compiles (e.g., via mvn) use JDK 8 since that's what my JAVA_HOME is pointing to. (I'm not even sure where the version 11 it found came from; possibly from when I installed maven.)
Determining the difference between the JRE and JDK you're running has never been straightforward. Seems like java -version used to be a way to do this, but no longer.
Adding to the complexity, you can also supposedly get your Java version info from Control Panel > Programs > Java > About. For me, that shows Version 8. That's despite java -version showing version 11.0.15. And it doesn't change even if I point my JAVA_HOME to JDK 11.
Note that this answer is also helpful. In my case, that helped me determine that I have java.exe and javac.exe at C:\Program Files (x86)\Common Files\Oracle\Java\javapath and C:\Program Files\Common Files\Oracle\Java\javapath. Depending on which one I have listed first in my Path variable, I get different results when i run java -version or java --version. The former seems to work when Java 8 is listed first; the latter when Java 11 is first.

Android studio not recognized jvm even if i already installed the jdk

First of all, I know that this question has been asked before maybe more than once, like this one for instance Android Studio - No JVM Installation found but i couldn't know the path that i have to refered to.
i already installed jdk-8u31-windows-x64 and i am working on windows 7 64 bit, when i open Android Studio, i got message:
it seems obvious that i should add a JAVA_HOME system variable, but what is the value of that variable?
after installing jdk-8u31-windows-x64, i found this folder on my computer
C:\Program Files (x86)\Java\jre1.8.0_31, should I refered to it? or to the bin inside it? or should I install other stuff?
Note 1:
when I type java -version on my cmd, I got this:
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
Note 2:
Yesterday I formatted my laptop and installed the latest version of java from the official website of java
The JAVA_HOME should contain the path of the 64-bit JDK.
C:\Program Files (x86)\Java\jre1.8.0_31 is the path of a JRE. Normally the JDK directory should be something like jdk1.8.0_31 in your case, and the JAVA_HOME would store the absolute path of this directory, without the bin directory. Try searching for that directory under C:/Program Files (C:/Program Files(x86) is usually for 32-bit installations).
It would be better to also install JDKs in a path that does not have space.

Cannot use Java 7 installation if Java 8 is installed

I normally still use Java 7 for all my coding projects (it's a company "politics" issue), but I installed Java 8 for one third-party project I am contributing to. Now, it seems I cannot have Java 8 installed in Windows 7 x64, and still use Java 7 by default:
C:\>"%JAVA_HOME%\bin\java.exe" -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
C:\>java.exe -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
As you can see, JAVA_HOME is completely ignored.
I also have Java in the path, using "%JAVA_HOME%\bin", which resolve correctly to Java 7 when I check the path in a DOS box, but it still makes no difference.
I checked in the "Java Control Panel" (not sure if this affects the default command-line Java version). Under the "Java" tab, the "View..." button, you get to see "registered" Java versions. I can add all the versions under the "User" tab, but under "System" there is only Java 8, and no way to change it.
Am I missing something, or did Oracle just make it impossible to use Java 7, unless I de-install Java 8? I don't want to have to specify the "source" and "target" everywhere, and I don't even know if it is possible for me to specify it everywhere, where Java is used.
EDIT: What I did is I de-installed all Java. Then installed the latest Java7 (both 86 and x64), and then the latest Java8 (both 86 and x64). After I did that, I noticed that the x64 JDK was gone. It seems Java8 killed it. So I re-installed the JDK 7 x64, after the JDK 8 x64. Still, JDK7 x64 did not seem to "replace" the "java.exe" which is copied into the "Windows" directory itself (I assume THAT is the problem).
When you install jdk8 it adds an entry like this
C:\ProgramData\Oracle\Java\javapath
to beginning of your PATH environment variable, removing this entry should resolve your problem.
You can select the JRE version from the command line with the -version: option.
> java -version:"1.7" MyClass
should select the 1.7 JRE if installed properly.
The list of the properly installed JRE is in the registry, see the key :
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment (32bit)
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment (64bit)
You can set the "CurrentVersion" there if you want a different default version than the latest.
See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html#options
Don't modify your PATH to point to a particuliar JRE, let the special java.exe in Windows/system32 do the job.
Windows and Unix both find programs using their PATH environment variable. You have an java.exe in your Windows\System32 which is appearing before your "preferred" version of Java.
Change the PATH to be the one you need, or specify the full path when you need a different version.
2 Steps
1
Change registry key **HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion** to point to 1.7
2
Copy java.exe,javaw.exe and javaws.exe from your java 1.7 version to Windows\System32 folder
(Since the corresponding files of java 1.8 are already there, you might have to overwrite with admin permissions)
3
(OOps actually not required 3rd step )
Open a new cmd window and check
java -version
Looks like you have to check where in your PATH is located your JAVA_HOME variable, the PATH is evaluated from left to right. A tip for you is to do all your Java system variables configuration at the beginning of your PATH.
PATH = %M2_HOME%\bin;%JAVA_HOME%\bin;C:\ProgramData\Oracle\Java\javapath;...
Probably that's why after doing this:
- java -version
you are getting this:
- java version "1.8.0_05"
because there are other areas in your PATH that are pointing to other java.exe, for example C:\Windows\System32 or C:\ProgramData\Oracle\Java\javapath etc.
I had to make 2 changes for it to work:
Changed the Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' to 1.7 from 1.8
The Java 8 installation adds a new entry to the PATH environment variable 'C:\ProgramData\Oracle\Java\javapath'. I removed this entry from the PATH.

Two installed javas on same pc. The one with lower version can not be called

I have two javas installed on my pc. jdk1.7.0_45 (x64) and jdk1.6.0_45(x86). I want that default java on my machine would be jdk1.6.0_45(x86). I added JAVA_HOME with value C:\Program Files (x86)\Java\jdk1.6.0_45 and added C:\Program Files (x86)\Java\jdk1.6.0_45\bin to system path, but still when i type in command prompt "java -version" it says:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
I have no idea why the things are like that, because my OS should see jdk1.7.0_45 (x64) version. Why is that so?
I use windows 8
edit:
after setting java home to jdk1.7.0_45 (x64) folder and updating path accordingly javac works, but then I set java home and path to jdk1.6.0_45(x86) javac does not work.
You need to add the %JAVA_HOME%\bin to the very beginning of your path. This is because Java is installed in your %SystemRoot%\system32, too. If you add the %JAVA_HOME%\bin to the beginning of your path the Java installed in your JAVA_HOME will be used.
JAVA_HOME does not help when we run java from command line, you should fix PATH system var
I added JAVA_HOME with value C:\Program Files (x86)\Java\jdk1.6.0_45
In Windows, you should excape spaces in the path: JAVA_HOME="C:\Program Files (x86)\Java\jdk1.6.0_45". But the best way is to set Java to a folder that doesn't have spaces in its path. For example, I use C:\Java\Java6
and added C:\Program Files (x86)\Java\jdk1.6.0_45\bin to system path
Probably you added after path to the Java 7. Make sure that Java 7 doesn't exist in the system path.

Javac and java pointing to different environments

Please Help,
I am trying to run a compiled java class and getting errors but when I try to check my java environments it points different ways as seen below
c:\NetBeansProjects\Hello\src>javac -version
javac 1.7.0
c:\NetBeansProjects\Hello\src>java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)
According to my PC(windows 7) I have
C:\Program Files (x86)\Java
jdk1.6.0_25
jdk1.7.0
jre6
jre7
How can I point it all to Java 7 or only Jave 6.....just want to try java 7 to see the fastness compared to java 6...hope all I have written helped.
Cheers.
Look at your path - I suspect c:\Windows\System32 is ahead of the JDK7 directory... and I suspect that's Java 6 for whatever reason.
You have the jre/bin directory on the system path before the jdk/bin. The javac command doesn't exist in the jre installation.
Thus the java command gets the version under jre6 but javac gets the version under jdk1.7.0.
You should change your system path to only include the one you want. If you want to explicitly use one over the other use the absolute name (including path) instead of just the executable name.
In the PATH variable enter C:\Program Files (x86)\Java\jdk1.6.0_25\bin
before the path of system32.
It fixed my problem
Just make sure java's path is the first path in the "PATH" environment variable
In all likelyhood, you have installed a JDK 7 and a JRE 6 and in your PATH environment variable the JRE bin path is before your JDK bin path
I'ts just because of your path, JRE does not contain javac and it contains java, so in your path the JRE must be located before the JDK

Categories