Environment variables for java installation
How to set the environment variables for Java in Windows (the classpath)?
Java SE Development Kit 8u112 on a 64-bit Windows 7 or Windows 8
Set the following user environment variables (== environment variables of type user variables)
-
JAVA_HOME :
C:\Program Files\Java\jdk1.8.0_112
-
JDK_HOME :
%JAVA_HOME%
-
JRE_HOME :
%JAVA_HOME%\jre
-
CLASSPATH :
.;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
-
PATH :
your-unique-entries;%JAVA_HOME%\bin
(make sure that the longishyour-unique-entries
does not contain any other references to another Java installation folder.
Note for Windows users on 64-bit systems:
Progra~1 = 'Program Files'
Progra~2 = 'Program Files(x86)'
Notice that these environment variables are derived from the "root" environment variable JAVA_HOME
. This makes it easy to update your environment variables when updating the JDK. Just point JAVA_HOME
to the fresh installation.
There is a blogpost explaining the rationale behind all these environment variables.
Optional recommendations
- Add an user environment variable
JAVA_TOOL_OPTIONS
with value-Dfile.encoding="UTF-8"
. This ensures that Java (and tools such as Maven) will run with aCharset.defaultCharset()
ofUTF-8
(instead of the defaultWindows-1252
). This has saved a lot of headaches when wirking with my own code and that of others, which unfortunately often assume the (sane) default encoding UTF-8. - When JDK is installed, it adds to the system environment variable
Path
an entryC:\ProgramData\Oracle\Java\javapath;
. I anecdotally noticed that the links in that directory didn't get updated during an JDK installation update. So it's best to removeC:\ProgramData\Oracle\Java\javapath;
from thePath
system environment variable in order to have a consistent environment.
In Windows inorder to set
Step 1 : Right Click on MyComputer and click on properties .
Step 2 : Click on Advanced tab
Step 3: Click on Environment Variables
Step 4: Create a new class path for JAVA_HOME
Step 5: Enter the Variable name as JAVA_HOME and the value to your jdk bin path ie c:\Programfiles\Java\jdk-1.6\bin and
NOTE Make sure u start with .;
in the Value so that it doesn't corrupt the other environment variables which is already set.
Step 6 : Follow the Above step and edit the Path in System Variables add the following ;c:\Programfiles\Java\jdk-1.6\bin
in the value column.
Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type
java
who will get a list of help doc
In order make sure whether compiler is setup Type in cmd
javac
who will get a list related to javac
Hope this Helps !
--- To set java path ---
There are two ways to set java path
A. Temporary
- Open cmd
- Write in cmd :
javac
If java is not installed, then you will see message:
javac is not recognized as internal or external command, operable program or batch file.
- Write in cmd :
set path=C:\Program Files\Java\jdk1.8.0_121\bin
- Write in cmd :
javac
You can check that path is set if not error has been raised.
It is important to note that these changes are only temporary from programs launched from this cmd.
NOTE: You might have to run the command line as admin
B. Permanent
- Righ-click on "My computer" and click on properties
- Click on "Advanced system settings"
- Click on "Environment variables"
- Click on new tab of user variable
- Write
path
invariable name
- Copy the path of bin folder
- Paste the path of the bin folder in the
variable value
- Click OK
The path is now set permanently.
TIP: The tool "Rapid Environment Editor" (freeware) is great for modifying the environment variables and useful in that case
TIP2: There is also a faster way to access the Environment Variables: press Win+R keys, paste the following %windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
and press ENTER
In Windows 7, right-click on Computer -> Properties -> Advanced system settings; then in the Advanced tab, click Environment Variables... -> System variables -> New....
Give the new system variable the name JAVA_HOME
and the value C:\Program Files\Java\jdk1.7.0_79
(depending on your JDK installation path it varies).
Then select the Path
system variable and click Edit.... Keep the variable name as Path
, and append C:\Program Files\Java\jdk1.7.0_79\bin;
or %JAVA_HOME%\bin;
(both mean the same) to the variable value.
Once you are done with above changes, try below steps. If you don't see similar results, restart the computer and try again. If it still doesn't work you may need to reinstall JDK.
Open a Windows command prompt (Windows key + R -> enter cmd
-> OK), and check the following:
java -version
You will see something like this:
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Then check the following:
javac -version
You will see something like this:
javac 1.7.0_79
The JDK installation instructions explain exactly how to set the PATH
, for different versions of Windows.
Normally you should not set the CLASSPATH
environment variable. If you leave it unset, Java will look in the current directory to find classes. You can use the -cp
or -classpath
command line switch with java
or javac
.