Here’s my guide for Windows 10.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as JAVA_8_HOME
JAVA_8_HOME
:%ProgramFiles%Javajdk1.8.0_152bin
JAVA_9_HOME
:%ProgramFiles%Javajdk-9.0.1bin
JAVA_HOME
:%JAVA_8_HOME%
In my case, JAVA_8_HOME
(JDK8) is pointing to C:Program FilesJavajdk1.8.0_152bin
. You can replace this with your own path to javac
. %JAVA_HOME%
has a default value pointing to JAVA_8_HOME
, which is the path for JDK8. That’s my preference, feel free to adjust accordingly.
Step 3. Select PATH
and click on Edit
. PATH
Step 4. Click on New
and add %JAVA_HOME%. %JAVA_HOME%
will be added to PATH
automatically every time you launch a command prompt.
In order to switch JDK version in cmd, here’s the trick.
Step 5. I created a batch file with
@echo off
:: Switch JDK version
DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%;
DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after ::
is the comments. Every time, java8
or java9
is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat
. You can name it whatever you want.
Step 6.
Search for regedit
(Registry Editor). Click on Edit
> New
> String Value
. Give AutoRun
as the Value name
and %USERPROFILE%profile.bat
as the Value data
. Here, please put your actual path value to the profile.bat
we just created. So, whenever a command prompt is opened, it automatically loads profile.bat
, which creates those two alias in the script.
Step 7. Close any command prompt you’re using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you’re using different Python versions, same trick applies, too. Find my python environment settings here.
Skip to content
If you are a windows user, you may have come across situations like you need to set environment veriables like JAVA_HOME but not having enough permission to access “Advanced system settings” option where you can dirrectly change them using the wizard or throught the “registry editor”.
But setx program can resolve your problem as it helps you to change environment valiable though Command Prompt
The setx
command is similar to the UNIX
utility SETENV
.
See below example
C:>setx JAVA_HOME <PATH>
When we set the variable
C:>setx JAVA_HOME "C:Program FilesJavajdk1.6.0_26" SUCCESS: Specified value was saved.
Note
When you change an environment variable by using the Windows setx command, you must close and reopen the Command Prompt window for the change to take effect.
We can check value of the set variable
C:>echo %JAVA_HOME% C:Javajdk1.6.0_26
Resource
http://technet.microsoft.com/en-us/library/cc755104(v=ws.10).aspx
- Details
- Written by
- Last Updated on 27 April 2019 | Print Email
In this article, you will understand the details about JAVA_HOME environment variable and how to set it on Windows 10.
What is JAVA_HOME?
By convention, JAVA_HOME is the name of an environment variable on the operating system that points to the installation directory of JDK (Java Development Kit) or JRE (Java Runtime Environment) – thus the name Java Home. For example:
JAVA_HOME = c:Program FilesJavajdk1.8.0_201
Why is JAVA_HOME needed?
To develop Java applications, you need to update the PATH environment variable of the operating system so development tools like Eclipse, NetBeans, Tomcat… can be executed because these programs need JDK/JRE to function. So the PATH environment variable should include JAVA_HOME:
PATH = Other Paths + JAVA_HOME
Other paths are set by various programs installed in the operating system. If the PATH environment variable doesn’t contain a path to JRE/JDK, a Java-based program might not be able to run. For example, typing java in the command prompt showing this error:
'java' is not recognized as an internal or external command, operable program or batch file.
Or Eclipse will fail to launch:
How to set JAVA_HOME on Windows 10
Here are the visual steps to properly set value for the JAVA_HOME and update the PATH environment variables in order to setup Java development environment on your computer:
1. Firstly, you need to identify the Java home directory, which is typically under C:Program FilesJava directory. Open My Computer and navigate to this directory, you will see:
Here, the home of JDK is under C:Program FilesJavajdk1.80_201. The version number may vary, depending on the JDK you installed.
2. Open the System Environment Variables dialog by typing environment in the search area on Start menu. Click the suggested item Edit the system environment variables:
The System Properties dialog appears, click the button Environment Variables.
Then you will see this dialog:
3.Create the JAVA_HOME environment variable by clicking the New button at the bottom. In the New System Variable form, enter the name and value as follows:
Click OK, and you will see the JAVA_HOME variable is added to the list.
4.Update the PATH system variable. In the Environment Variables dialog, select the Path variable and click Edit:
Then in the Edit environment variable dialog, double click on the empty row just below the last text line, and enter %JAVA_HOME%bin as follows:
The percent signs tell Windows that it refers to a variable – JAVA_HOME, and the bin specifies the location of java.exe and javac.exe programs which are used to run and compile Java programs, as well as other tools in the JDK.
Click OK button to close all the dialogs, and you’re all set. Now you can open Eclipse or NetBeans to verify. Or open a command prompt and type in javac –version, you should see:
NOTES:
You can add the path to the bin directory of Java home directly into the PATH variable. But it’s strongly recommend to follow the above steps to setup a JAVA_HOME variable because many Java programs depend on it.
When you installed a JDK, you might not need to update the system variable because the installer already did it for you.
Learn more:
- How to set environment variables for Java using command line
- How to write, compile and run a hello world Java program for beginners
- What are JVM, JRE and JDK
- How to check Java version
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.