209

How to set the environment variables for Java in Windows (the classpath)?

Johannes
  • 178
  • 11
Dean jones
  • 2,113
  • 3
  • 13
  • 4

15 Answers15

411

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 longish your-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 a Charset.defaultCharset() of UTF-8 (instead of the default Windows-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 entry C:\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 remove C:\ProgramData\Oracle\Java\javapath; from the Path system environment variable in order to have a consistent environment.
Amin Soheyli
  • 471
  • 1
  • 5
  • 15
Abdull
  • 23,005
  • 22
  • 116
  • 159
  • 12
    It's the better answer – Kimchi Man Feb 24 '15 at 15:35
  • 1
    Good answer. I also make a directory junction to my java installation that I can update whenever I install a new version (`mklink /j C:\devel\java8 "C:\Program Files\Java\jdk1.8.0_91"`). That way when I use cygwin or something that can't read windows env variables, I still have a quick way of swapping versions. And it's much easier to remember the shorter path without the minor version. – kghastie Jul 15 '16 at 19:00
  • I tried setting a JAVA_ROOT_PATH variable but JAVA_HOME didn't seem to like me using any environment variable for it's value. Is there a reason for this? – Eric Nov 05 '16 at 20:21
  • @Eric, can you explain what you mean with `JAVA_ROOT_PATH`? I haven't heard of this environment variable yet. – Abdull Nov 06 '16 at 21:18
  • 4
    The `javapath` directory and symbolic links are created by the JRE installation (which is an optional process when installing the JDK) so if you didn't install the JRE you wouldn't have created javapath. Note that the `%JAVA_HOME%\jre` in your answer is the JDK's private JRE intended for the use of the Java compiler. It's not exactly the same as the public JRE. Also note that, as of 1.8u112, the JRE installation seems to have stopped creating symbolic links in `javapath` and instead creates hard links to .exes in `..\javapath_target_nnnnnnn\*.exe`. – Klitos Kyriacou Nov 21 '16 at 11:12
  • @KlitosKyriacou, thank you, I didn't yet know about the distinction *public vs. private JRE*. I compared both folder hierarchies and found only minor differences. So I guess it's save to set `JRE_HOME=%JAVA_HOME%\jre` whether or not the optional *JRE* is installed. In case there are objections, please comment. – Abdull Nov 21 '16 at 14:06
  • @Abdull I made a JAVA_ROOT_PATH env variable myself. I was trying to come up with a way to nest environment variables so if I ever updated java I would only have to update a JAVA_ROOT_PATH env variabe and change it's location. This would only work if you kept a static directory structure that held the jdk and jre. Was just an attempt at over engineering something that should be kept simple. :) – Eric Jan 05 '17 at 15:40
  • I had to move my jre folder inside my jdk folder – cegprakash Sep 27 '17 at 18:32
  • For me worked "System" environment variables, instead of User environment variables – MauricioTL Nov 15 '17 at 18:21
  • actually there is no need to add the java lib directory to the CLASSPATH (not more since a long time) – user85421 Jan 30 '18 at 06:54
  • There's no jre folder on jdk 10 – Newbie Apr 09 '18 at 07:24
  • Please add the removal of the java.exe file in the system32 directory. This step is needed when installing multiple versions of java on a computer. In that case only the most recent version can be used. – sarcode Jul 19 '18 at 13:26
  • after setting the above, reboot would be advised – Mark Burleigh Aug 22 '18 at 19:32
  • 4
    The setting works well for me. As there is no jre folder on jdk 11, I remove the JRE_HOME variable as well as the %JAVA_HOME%\jre\lib value in the CLASSPATH variable. For more info on jdk 11 without jre: https://stackoverflow.com/questions/52584888/how-to-use-jdk-without-jre-in-java-11 – Huanfa Chen Feb 12 '19 at 10:54
  • @Abdull There is no `C:\ProgramData\Oracle\Java\javapath;` variable in the Path, but only `C:\Program Files (x86)\Common Files\Oracle\Java\javapath`. So, should we remove `C:\Program Files (x86)\Common Files\Oracle\Java\javapath` as well? –  Jul 28 '20 at 18:50
66

In Windows inorder to set

Step 1 : Right Click on MyComputer and click on properties .

Step 2 : Click on Advanced tab

alt text

Step 3: Click on Environment Variables

alt text

Step 4: Create a new class path for JAVA_HOME

alt text

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.

alt text

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 !

Taylor Hx
  • 2,667
  • 20
  • 35
Srinivas M.V.
  • 6,098
  • 5
  • 28
  • 49
  • 1
    Nice explanation. One small thing in step 6: Don't type `$java` or `$javac`, but just `java` or `javac`. – Jesper Nov 04 '09 at 09:43
  • 1
    And note that you must add the JDK bin directory to the `PATH` environment variable; just setting `JAVA_HOME` is not enough. – Jesper Nov 04 '09 at 09:44
  • 1
    yeah sorry since im a Linux lover . i missed it will change it , Thanks – Srinivas M.V. Nov 04 '09 at 09:44
  • 3
    1. use backslashes in WINDOWS 2. dont start any path with ".;" 3. add %JAVA_HOME%\bin to the path, like that you can later change the java_home without having to change the path – raudi Feb 12 '14 at 20:23
  • 1
    @Wannacoffee Updated the changes for backslashes – Srinivas M.V. Apr 10 '14 at 08:29
  • `JAVA_HOME` should not include the bin directory, it's home for Java at not just the executable – user85421 Jan 30 '18 at 06:57
30

--- To set java path ---

There are two ways to set java path

A. Temporary

  1. Open cmd
  2. 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.

  1. Write in cmd : set path=C:\Program Files\Java\jdk1.8.0_121\bin
  2. 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

  1. Righ-click on "My computer" and click on properties
  2. Click on "Advanced system settings"
  3. Click on "Environment variables"
  4. Click on new tab of user variable
  5. Write path in variable name
  6. Copy the path of bin folder
  7. Paste the path of the bin folder in the variable value
  8. 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

Jean-Francois T.
  • 9,065
  • 3
  • 46
  • 79
Kimmi Dhingra
  • 2,098
  • 19
  • 21
17

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
Arun
  • 1,932
  • 4
  • 20
  • 31
15

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.

Jesper
  • 186,095
  • 42
  • 296
  • 332
5

I am going to explain here by pictures for Windows 7.

Please follow the following steps:

Step 1: Go to "Start" and get into the "My Computer" properties

enter image description here

Step 2: Go to "Advance System Setting" and click on it.

enter image description here

Step 3: Go to "Start" and get into the "My Computer" properties

enter image description here

Step 4: The dialog for Environment variable will open like this:

enter image description here

Step 5: Go to path and click on edit.

enter image description here

Step 6: Put the path of your JDK wherever it resides up to bin like you can see in the picture. Also add path from your sdk of Android up to the Platform Tools:

enter image description here

hitesh141
  • 947
  • 10
  • 25
3

Under Linux: http://lowfatlinux.com/linux-environment-variables.html

And of course, you can retrieve them from Java using:

String variable = System.getProperty("mykey");
kenny_k
  • 3,325
  • 4
  • 24
  • 36
Lastnico
  • 1,405
  • 11
  • 13
3

In programming context you can execute SET command (SET classpath=c:\java) or Right click on your computer > properties > advanced > environment variables.

In a batch file you can use

SET classpath=c:\java
java c:\myapplication.class
Cem Kalyoncu
  • 12,631
  • 4
  • 38
  • 60
3

For Windows 7 users:

Right-click on My Computer, select Properties; Advanced; System Settings; Advanced; Environment Variables. Then find PATH in the second box and set the variable like in the picture below.

PATH variable editor

Victor Zamanian
  • 2,861
  • 21
  • 28
AndroidGeek
  • 30,803
  • 14
  • 212
  • 262
2

Keep in mind that the %CLASSPATH% environment variable is ignored when you use java/javac in combination with one of the -cp, -classpath or -jar arguments. It is also ignored in an IDE like Netbeans/Eclipse/IntelliJ/etc. It is only been used when you use java/javac without any of the above mentioned arguments.

In case of JAR files, the classpath is to be defined as class-path entry in the manifest.mf file. It can be defined semicolon separated and relative to the JAR file's root.

In case of an IDE, you have the so-called 'build path' which is basically the classpath which is used at both compiletime and runtime. To add external libraries you usually drop the JAR file in a (either precreated by IDE or custom created) lib folder of the project which is added to the project's build path.

Vishal Yadav
  • 3,351
  • 3
  • 20
  • 40
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • @Balus he is a begginer who is trying to set his java Environment on Windows PC . – Srinivas M.V. Nov 04 '09 at 12:00
  • 1
    Exactly. The one who would get greatly confused when he discovers that the classpath he configured doesn't work for JAR files nor IDE's. Just a warning at its place :) – BalusC Nov 04 '09 at 12:23
2

Set java Environment variable in Centos / Linux

/home/ vi .bashrc

export JAVA_HOME=/opt/oracle/product/java/jdk1.8.0_45

export PATH=$JAVA_HOME/bin:$PATH

java -version

Guna Sekaran
  • 467
  • 3
  • 5
1

For deployment better to set up classpath exactly and keep environment clear. Or at *.bat (the same for linux, but with correct variables symbols):

CLASSPATH="c:\lib;d:\temp\test.jar;<long classpath>"
CLASSPATH=%CLASSPATH%;"<another_logical_droup_of_classpath" 
java -cp %CLASSPATH% com.test.MainCLass

Or at command line or *.bat (for *.sh too) if classpath id not very long:

java -cp "c:\lib;d:\temp\test.jar;<short classpath>"
St.Shadow
  • 1,815
  • 1
  • 12
  • 16
1

For Windows:

  • Right click on 'My Computers' and open 'Properties'.
  • In Windows Vista or Windows 7, go to "Advanced System Settings". Else go to next step.
  • Go to 'Advanced Tab' and click on Environment Variables button.
  • Select 'Path' under the list of 'System Variables', and press Edit and add C:\Program Files\java\jdk\bin after a semicolon.
  • Now click on 'new' button under system variables and enter 'JAVA_HOME' as variable name and path to jdk home directory (ex. 'C:\Program Files\Java\jdk1.6.0_24' if you are installing java version 6. Directory name may change with diff. java versions) as variable_value.
Pratap Singh
  • 4,097
  • 1
  • 20
  • 22
1
  1. Download the JDK
  2. Install it
  3. Then Setup environment variables like this :
  4. Click on EDIT

enter image description here

  1. Then click PATH, Click Add , Then Add it like this: enter image description here
Abhishek Sengupta
  • 1,680
  • 1
  • 14
  • 26
0

Java path set for java 11

  1. copy the path for jdk-11

Don’t include the \bin folder, just the JDK path. For example

CorrectC:\Program Files\Java\jdk-11

WrongC:\Program Files\Java\jdk-11\bin

In environmental variable, user variable section click on New button and give path like below. enter image description here

after that give ok for it and go to the System variables and select the Path and double click on it.

enter image description here

click on new and paste %JAVA_HOME%\bin and click ok to all. enter image description here