How to set the environment variables for Java in Windows (the classpath)?
xpt
20.7k37 gold badges128 silver badges217 bronze badges
asked Nov 4, 2009 at 7:54
5
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 a 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.
starball
22.1k8 gold badges47 silver badges285 bronze badges
answered Oct 29, 2014 at 21:00
AbdullAbdull
26.5k26 gold badges130 silver badges172 bronze badges
15
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 !
Taylor Hx
2,81523 silver badges36 bronze badges
answered Nov 4, 2009 at 9:08
Srinivas M.V.Srinivas M.V.
6,5085 gold badges33 silver badges49 bronze badges
6
— 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
answered Mar 20, 2017 at 7:04
0
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
answered Nov 4, 2015 at 3:43
ArunArun
2,3125 gold badges24 silver badges33 bronze badges
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
.
answered Nov 4, 2009 at 8:26
JesperJesper
203k46 gold badges318 silver badges350 bronze badges
1
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
Step 2: Go to «Advance System Setting» and click on it.
Step 3: Go to «Start» and get into the «My Computer» properties
Step 4: The dialog for Environment variable will open like this:
Step 5: Go to path and click on edit.
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:
answered Jul 10, 2015 at 12:10
hitesh141hitesh141
93312 silver badges25 bronze badges
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
answered Nov 4, 2009 at 8:03
Cem KalyoncuCem Kalyoncu
14.2k4 gold badges41 silver badges62 bronze badges
1
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.
answered May 26, 2014 at 7:56
Zar E AhmerZar E Ahmer
34k20 gold badges236 silver badges300 bronze badges
Java path set for java 11
- copy the path for jdk-11
Don’t include the \bin folder, just the JDK path. For example
Correct – C:\Program Files\Java\jdk-11
Wrong – C:\Program Files\Java\jdk-11\bin
In environmental variable, user variable
section click on New button and give path like below.
after that give ok for it and go to the System variables
and select the Path
and double click on it.
click on new and paste %JAVA_HOME%\bin
and click ok to all.
answered May 6, 2021 at 22:10
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
answered Aug 9, 2016 at 4:42
0
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,6423 gold badges25 silver badges42 bronze badges
answered Nov 4, 2009 at 11:54
BalusCBalusC
1.1m373 gold badges3613 silver badges3557 bronze badges
2
- Download the JDK
- Install it
- Then Setup environment variables like this :
- Click on EDIT
- Then click PATH, Click Add , Then Add it like this:
answered May 16, 2019 at 11:04
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>"
answered Nov 4, 2009 at 8:26
St.ShadowSt.Shadow
1,8501 gold badge12 silver badges16 bronze badges
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.
answered Sep 6, 2013 at 11:14
Pratap SinghPratap Singh
4,6171 gold badge22 silver badges24 bronze badges
Your Keytools file sit under «Java/bin» folder so you need to either set Environment variable or go to «Java/bin» folder and run command
answered Aug 9, 2021 at 17:35
vaquar khanvaquar khan
10.9k5 gold badges73 silver badges96 bronze badges
You can add JAVA_HOME in the system environment variable from my computer>>advance tab>add the new path as explained here.
It might help Mac and Linux users as well.
answered Jun 12, 2022 at 5:14
SunilSunil
3,2193 gold badges21 silver badges21 bronze badges
Setting environment variables for Java in Windows allows you to specify certain configurations for the JVM (Java Virtual Machine) that runs your Java applications. This can be useful for setting the maximum heap size for the JVM, for example, or for specifying the location of Java-related files on your system. There are several ways to set environment variables in Windows, depending on the version of Windows you are using and the scope you want the variables to have.
Method 1: Setting environment variables through System Properties
To set the environment variables for Java in Windows through System Properties, follow the steps below:
- Open the Start menu and search for System Properties.
- Click on the Advanced tab, then click on the Environment Variables button.
- Under System Variables, click on the New button.
- In the Variable name field, enter
JAVA_HOME
. - In the Variable value field, enter the path to your Java installation directory. For example,
C:\Program Files\Java\jdk1.8.0_221
. - Click OK to close all windows.
Now that you have set the JAVA_HOME
environment variable, you can add it to the Path
environment variable by following these steps:
- Under System Variables, find the
Path
variable and click on Edit. - In the Variable value field, add
%JAVA_HOME%\bin;
to the beginning of the string. Note the semicolon at the end. - Click OK to close all windows.
To verify that the environment variables have been set correctly, open a command prompt and type java -version
. You should see the version of Java that you have installed.
Example code
// Get the value of the JAVA_HOME environment variable
String javaHome = System.getenv("JAVA_HOME");
// Use the value of JAVA_HOME to construct the path to the Java executable
String javaExecutable = javaHome + "\\bin\\java.exe";
// Construct a ProcessBuilder object to run a Java application
ProcessBuilder pb = new ProcessBuilder(javaExecutable, "-jar", "myapp.jar");
// Set the environment variables for the process
Map<String, String> env = pb.environment();
env.put("MY_VAR", "my value");
// Start the process
Process p = pb.start();
This example code shows how to get the value of the JAVA_HOME
environment variable, use it to construct the path to the Java executable, and set environment variables for a new process using a ProcessBuilder
object.
Method 2: Setting environment variables through Environment Variables
To set the environment variables for Java in Windows, you can use the «Environment Variables» dialog box in the System Properties settings. Here are the steps to set the environment variables for Java:
-
Open the «Environment Variables» dialog box by right-clicking on «My Computer» and selecting «Properties» from the context menu. Then, click on «Advanced system settings».
-
In the «System Properties» dialog box, click on the «Environment Variables» button.
-
In the «Environment Variables» dialog box, under «System Variables», click on «New».
-
In the «New System Variable» dialog box, enter «JAVA_HOME» as the variable name and the path to the Java installation directory as the variable value. For example, «C:\Program Files\Java\jdk1.8.0_181».
-
Click «OK» to close all the dialog boxes.
-
To verify that the environment variable has been set correctly, open a command prompt and type «echo %JAVA_HOME%». The command should output the path to the Java installation directory.
Here is an example code snippet that shows how to set the «JAVA_HOME» environment variable using Java:
public class SetJavaHome {
public static void main(String[] args) {
String javaHome = "C:\\Program Files\\Java\\jdk1.8.0_181";
System.setProperty("JAVA_HOME", javaHome);
System.out.println("JAVA_HOME is set to " + System.getenv("JAVA_HOME"));
}
}
The code sets the «JAVA_HOME» system property to the path of the Java installation directory and then retrieves the value of the «JAVA_HOME» environment variable using the System.getenv()
method.
In addition to the «JAVA_HOME» environment variable, you may also need to set the «PATH» environment variable to include the path to the Java executable. Here is an example code snippet that shows how to set the «PATH» environment variable using Java:
public class SetJavaPath {
public static void main(String[] args) {
String javaHome = "C:\\Program Files\\Java\\jdk1.8.0_181";
String javaBin = javaHome + "\\bin";
String path = System.getenv("PATH");
path += ";" + javaBin;
System.setProperty("PATH", path);
System.out.println("PATH is set to " + System.getenv("PATH"));
}
}
The code sets the «PATH» system property to include the path to the Java executable and then retrieves the value of the «PATH» environment variable using the System.getenv()
method.
That’s it! You have successfully set the environment variables for Java in Windows using the «Environment Variables» dialog box.
Method 3: Setting environment variables in the Command Prompt
To set environment variables for Java in Windows using the command prompt, follow these steps:
-
Open the command prompt by pressing
Windows key + R
, then typecmd
and pressEnter
. -
Type
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_221"
and pressEnter
. This sets the JAVA_HOME environment variable to the location of your Java installation. -
Type
setx PATH "%PATH%;%JAVA_HOME%\bin"
and pressEnter
. This appends the Java bin directory to your PATH environment variable, allowing you to run Java commands from anywhere in the command prompt. -
Verify that the environment variables have been set correctly by typing
echo %JAVA_HOME%
andecho %PATH%
.
Here’s what the code looks like:
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_221"
setx PATH "%PATH%;%JAVA_HOME%\bin"
echo %JAVA_HOME%
echo %PATH%
That’s it! With these environment variables set, you can now use Java commands in the command prompt.
- What Are Environment Variables and Why We Use It
- Set Environment Variables for Java in Windows
- Set Environment Variables for Java in macOS and Linux
Today, we will look at an important topic that can be helpful when we install the JDK or Java Development Kit in an operating system. Environment variables usually describe the environment and how things should behave in this environment. There are various variables available, but the most used variable is PATH. We will look deeply into this topic in the following sections.
What Are Environment Variables and Why We Use It
Environment Variables are key/value pairs that specify configurations for various programs in an operating system. Most of the popular operating systems like Windows, macOS, and Linux use it. These values are commonly Strings.
The common use of these variables is when we want to execute a program using a command without going to or writing the whole path to the program. For example, if we want to run a Java program anywhere, w6e may have to first go to the location where Java is located through the command-line or terminal, and then we can use Java.
But if we specify the path to Java in the environment variables, we have to use the program’s name or the command attached to it, and the program will be executed.
Set Environment Variables for Java in Windows
We can install Java or JDK in Windows using the install wizard that comes with it, but we have to set the environment variables manually. In the Windows operating system, we can access the settings for environment variables, edit them, or use the command line.
We will see the steps to set the variables using the command line.
-
First we get the location where Java is installed or the Java that we want to use globally. We can do this easily by using the following command in the command line.
Output:
C:\Users\User1\.jdks\openjdk-15.0.1\bin\java.exe
-
Now that we have the path, we use it to set the variables; there are two variables we need to configure that are the JAVA_HOME and PATH variables. To do this, we open the command line or CMD for short, and it is better to run it as administrator.
-
In the command line, we write the following command to set both variables:
setx JAVA_HOME "C:\Users\User1\.jdks\openjdk-15.0.1" setx PATH "C:\Users\User1\.jdks\openjdk-15.0.1\bin\";
Here
setx
sets the environment variables while JAVA_HOME and PATH are the variable names, and then comes the value of the variable.
It will set or update the variables. Note that JAVA_HOME
uses the path where the whole Java folder is situated, while PATH
is the path of the subfolder called \bin where Java.exe and other executables are located.
Set Environment Variables for Java in macOS and Linux
In the macOS and Linux, we can use the following steps to set or update the environment variables for Java:
-
Open the terminal
-
identify the default shell that is being used by the OS using this command:
Output:
This will print the default shell. Note that different versions of the operating system may have different shells; for this tutorial, we use the
zsh
shell. -
Open
./zshenv
that is used to set environment variables in thezsh
shell using the below command: -
Now add the following content.
export JAVA_HOME=$(/usr/libexec/java_home)
-
Now to execute the script or the content that we just added, we use the below command:
-
At last, to check if the variable was set, we will print
JAVA_HOME
.Output:
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
If you have installed Java on your Windows 11 and facing an error due to an environment variable, you are at the right place. It can be caused if you have not set the environment variable for the Java bin folder path. Here we will discuss; how to set up the environment variable on Windows 11.
The environment variable is a variable that is used by the system to locate the installation path and other system preferences. There are two types of variables: system variables and user variables. One is system-specific, and the other one is user-specific. They play an important role in the Windows operating system.
By just downloading and installing Java SE Development Kit on your system, Java files will not work. You have to specify the Java installation path on the environment variable. So, the system can execute Java code on your Windows 11 system.
If you prefer a video tutorial, you can watch the video mentioned below.
Here are the steps to set up an environment variable for Java:
1. Open C Drive > Program Files folder look for the Java folder and open it.
2. Inside the Java folder, you will find a JDK folder. Open it and open the bin folder.
3. Now copy the path of the bin folder location.
4. After that, click on the Windows icon to open the Start Menu and click on Settings.
5. Next, click on the System tab from the left panel of the Settings window.
6. Then, look for an option called About and click on that option present to the right of your screen.
7. Now, you will get all your device specifications. Click on the Advanced system settings option; to access the System Properties.
8. It will open the system properties window. Click the Advanced tab and select Environment Variables to open the variables windows.
9. You will get two variables option. One is for user variables and system variables. Under the system variable section, select the Path variable and click on the Edit button.
10. Now, click New and add the bin folder location.
11. After that, click on OK to save the changes.
12. To verify, open the command prompt and run the following commands one by one.
java --version
javac
13. If the command returns the currently installed version of Java, that means; the Java environment variable is fully set on your Windows system.
Conclusion
That’s it; this is how you can set up environment variables for Java in Windows 11. This will fix any Java error you are having on your system. However, if you are getting a JAVA_HOME error then you can check out our detailed guide on how to set the JAVA_HOME variable on Windows 11.
We hope this article was helpful to you. Moreover, if you have some suggestions, do not hesitate to leave them in the comments section below.
He is a prominent tech writer with over six years of experience and the founder of TheCoderWord. He delivers high-quality content revolving around troubleshooting and how-to guides for Windows, Linux, macOS, Chrome, and more.
- Details
- Written by
- Last Updated on 09 April 2020 | Print Email
Why do you need to setup environment variables in Java programming?
Well, in this article I talked about the JVM, JRE and JDK, which are the cornerstones of the Java programming language. You develop Java applications by using tools like editors, IDEs and servers. These tools need to use the Java compiler (javac) and Java launcher (java) to compile and run Java applications, but these tools don’t know where the JRE or JDK is.
So, by setting up the environment variables, you tell your tools that:
“Hey, you can find the compiler and launcher here and there”.
The first thing you need to do after installing the JDK is creating an environment variable named JAVA_HOME and then update the PATH variable.
- JAVA_HOME: stores location of the JDK’s installation directory. When you install development tools, they will first check for the JAVA_HOME variable. If found, they will stick with it. If not, they may ask you to manually specify the location where JRE/JDK is installed.
- PATH: stores paths of directories where the operating system will look, to launch the requested programs quickly. For Java development, you should update this environment variable by adding an entry to the bin directory under JDK’s installation directory.
For example, if the JDK is installed at C:\Program Files\Java\jdk1.8.0, then you need to set:
- JAVA_HOME = C:\Program Files\Java\jdk1.8.0
- PATH = PATH + C:\Program Files\Java\jdk1.8.0\bin
Setting these environment variables on Windows is not difficult. Just go to Control Panel > System > Advanced system settings > Advanced > Environment Variables. You can see the whole steps to setup this stuff in the following article:
How to write, compile and run a hello world Java program for beginners
But that isn’t cool, because I’m about to show you how to do the same thing using command line prompt as shown below (Windows 7, 8 and 10):
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx PATH "%PATH%;%JAVA_HOME%\bin";
The setx command permanently updates the environment variables. To add/update system environment variables, you must use the -m switch and open the command prompt using Administrator privilege: Click Start, type cmd. When the cmd.exe icon appears, right click and select Run as administrator.
To add/update system environment variables:
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -m PATH "%PATH%;%JAVA_HOME%\bin";
I prefer setting the environment variables using this command-line alternative. It’s quick and easy instead of going through several dialogs like using the GUI.
To summary, here are some important points:
- Always set JAVA_HOME when preparing development environment for Java.
- JAVA_HOME points to the installation directory of JDK.
- The PATH variable should contain an entry pointing to JAVA_HOME\bin.
- Environment can be set either via the GUI or command line prompt.
I hope that you have learned something new today. You can also watch the following video for visual steps:
Related Tutorials:
- How to set JAVA_HOME environment variable on Windows 10
- How to check Java version
- Understand 9 features of Java programming language
- How to write, compile and run a hello world Java program for beginners
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.