- How to set the environment variables for Java in Windows
- 17 Answers 17
- Java SE Development Kit 8u112 on a 64-bit Windows 7 or Windows 8
- Optional recommendations
- Environment Variables
- Querying Environment Variables
- Passing Environment Variables to New Processes
- Platform Dependency Issues
- How do I set environment variables from Java?
How to set the environment variables for Java in Windows
I am alone in thinking it’s too lame that the java installer doesn’t update the path environment variable automatically?
@ThomasEyde — only problem with that idea is what if I want multiple JVMs on my machine — auto setting the path would then cause problems.
17 Answers 17
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.
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 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.
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.
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?
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 .
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: stackoverflow.com/questions/52584888/…
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
who will get a list of help doc
In order make sure whether compiler is setup Type in cmd
who will get a list related to javac
And note that you must add the JDK bin directory to the PATH environment variable; just setting JAVA_HOME is not enough.
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
There are two ways to set java path
A. Temporary
If java is not installed, then you will see message:
javac is not recognized as internal or external command, operable program or batch file.
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 in variable 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:
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)
You will see something like this:
Environment Variables
Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters. To learn how to pass environment variables to applications on your system, refer to your system documentation.
Querying Environment Variables
On the Java platform, an application uses System.getenv to retrieve environment variable values. Without an argument, getenv returns a read-only instance of java.util.Map , where the map keys are the environment variable names, and the map values are the environment variable values. This is demonstrated in the EnvMap example:
import java.util.Map; public class EnvMap < public static void main (String[] args) < Mapenv = System.getenv(); for (String envName : env.keySet()) < System.out.format("%s=%s%n", envName, env.get(envName)); >> >
With a String argument, getenv returns the value of the specified variable. If the variable is not defined, getenv returns null . The Env example uses getenv this way to query specific environment variables, specified on the command line:
Passing Environment Variables to New Processes
When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application’s virtual machine process. The application can change this set using ProcessBuilder.environment .
Platform Dependency Issues
There are many subtle differences between the way environment variables are implemented on different systems. For example, Windows ignores case in environment variable names, while UNIX does not. The way environment variables are used also varies. For example, Windows provides the user name in an environment variable called USERNAME , while UNIX implementations might provide the user name in USER , LOGNAME , or both.
To maximize portability, never refer to an environment variable when the same value is available in a system property. For example, if the operating system provides a user name, it will always be available in the system property user.name .
How do I set environment variables from Java?
To a system property from Java, you can use the System.setProperty() method. However, keep in mind that this method only sets system properties, which are different from environment variables.
System properties are a set of key-value pairs that can be set at the Java Virtual Machine (JVM) level, and are used to configure the behavior of the JVM and Java applications. They are stored in the java.lang.System class, and can be accessed using the System.getProperty() method.
To set a system property from Java, you can use the System.setProperty() method, like this:
System.setProperty("propertyName", "propertyValue");
System.setProperty("java.io.tmpdir", "/tmp");
This will set the java.io.tmpdir system property to the value «/tmp» .
Environment variables, on the other hand, are system-wide variables that are set outside of the JVM and are used to configure the operating system and other programs. They can be accessed using the System.getenv() method.
To set an environment variable from Java, you will need to use a native method or a third-party library that allows you to set environment variables.
One way to set environment variables from Java is to use the ProcessBuilder class. The ProcessBuilder class allows you to start a new process with a modified environment, which inherits the current process’s environment and allows you to make changes to it. Here’s an example of how you can set an environment variable using ProcessBuilder:
import java.io.IOException; import java.util.Map; public class SetEnvironmentVariable < public static void main(String[] args) < try < // Create a new ProcessBuilder instance ProcessBuilder processBuilder = new ProcessBuilder(); // Get the environment variables from the ProcessBuilder instance Map environment = processBuilder.environment(); // Set a new environment variable environment.put("MY_ENV_VAR", "MyEnvironmentVariableValue"); // Run a command with the modified environment processBuilder.command("someCommand"); Process process = processBuilder.start(); process.waitFor(); > catch (IOException | InterruptedException e) < e.printStackTrace(); >> >
In this example, we first create a new ProcessBuilder instance. Then, we retrieve the environment variables from the ProcessBuilder by calling the environment() method. Next, we set a new environment variable by calling the put() method on the Map of environment variables. Finally, we run a command using the start() method, which launches a new process with the modified environment.
Please note that this approach sets the environment variable for the new process and its child processes, but not for the current JVM process. The new environment variable will not be accessible from the current JVM process using System.getenv() .