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 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.
Answer from Abdull on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › env.html
Environment Variables (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
To learn how to pass environment variables to applications on your system, refer to your system documentation. 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.
Top answer
1 of 16
458

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 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.
2 of 16
70

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 !

Discussions

Set environment variable using Java
I think you could set variables in two ways: 1.) using command line: java -Dvar1=value1 - Dvar2=value1... com.package.prog1 2.) using Docker to run your app and set variables like this More on reddit.com
🌐 r/java
6
0
April 12, 2020
Installation problem on MacOS - JAVA_HOME environment variable is not defined correctly
Does this help? https://community.atlassian.com/t5/Jira-questions/How-do-I-fix-my-Java-for-Mac-installation-after-installing-the/qaq-p/1169523 More on reddit.com
🌐 r/jira
3
2
July 27, 2023
Java Environment PATH Variable Setup
This will help people if they messed up their Java. More on reddit.com
🌐 r/Minecraft
7
6
October 26, 2015
Setting JAVA_HOME path
It means that you have not configured the JAVA_HOME environment variable. Using a virtual terminal application, type in: update-alternatives --config java, which will give you the location of JDK listed under Path column. Depending on the installed version of JDK, it will be something like: /usr/lib/jvm/java-14-openjdk-amd64/bin/java If you have installed multiple versions of JDK, all of them will be listed here 2. Using text editor such as nano and terminal emulator, type in: nano /etc/profile.d/jdk_home.sh This will create and open for editing a new script file named jdk_home.sh under the location /etc/profile.d, where you need to type in: export JAVA_HOME= where JDK-Location should not include the /bin/java directories. For example: EXPORT JAVA_HOME=”/usr/lib/jvm/java-14-openjdk-amd64” This will set your environment variable JAVA_HOME The steps above are if you have installed JDK using apt package manager tool, you also have the option of using SDKMAN which will configure all environment variables for you automatically. More on reddit.com
🌐 r/vscode
12
1
February 27, 2020
🌐
Oracle
java.com › en › download › help › path.html
How do I set or change the PATH system variable?
Making changes to the system PATH variable is typically not necessary for computers running Windows or Mac OS X. In Search, search for and then select: System (Control Panel) Click the Advanced system settings link. Click Environment Variables. In the section System Variables find the PATH ...
🌐
Vonage
developer.vonage.com › en › blog › working-with-environment-variables-in-java
Working with Environment Variables in Java
Environment variables, such as ENV_MODE or APP_ENV, can indicate whether the application is running in development, testing, staging, or production environments. This allows applications to adjust their behavior based on context automatically.
🌐
GeeksforGeeks
geeksforgeeks.org › java › environment-variables-in-java
Environment Variables in Java - GeeksforGeeks
January 7, 2026 - Environment variables in Java are key–value pairs maintained by the operating system and accessed by the JVM at runtime. They are widely used in Java to provide external configuration to applications.
🌐
GeeksforGeeks
geeksforgeeks.org › java › setting-environment-java
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME - GeeksforGeeks
May 9, 2025 - Now, go to Environment Variable > System Variable section > click New. ... In the Variable name field, enter JAVA_HOME and in the Variable value field enter the path where the JDK is installed.
🌐
CodeGym
codegym.cc › java blog › core java › get environment variables in java
Get Environment variables in Java
January 9, 2025 - Passing sensitive information through environment variables can pose security risks. Use secure mechanisms, such as encrypted values or temporary variables, to mitigate these risks when handling sensitive data. import java.io.IOException; public class ApplicationEnvironmentSetup { public static void main(String[] args) { try { // Create a ProcessBuilder for a mock application ProcessBuilder processBuilder = new ProcessBuilder("java", "-version"); // Set environment variables specific to the application processBuilder.environment().put("APP_ENV", "Production"); processBuilder.environment().put("API_KEY", "12345-secure-key"); // Start the process Process process = processBuilder.start(); // Print the output process.getErrorStream().transferTo(System.out); // For commands like java -version } catch (IOException e) { System.err.println("Error starting process: " + e.getMessage()); } } }
Find elsewhere
🌐
Configu
configu.com › home › working with java environment variables & 4 ways to set variables
Working with Java Environment Variables & 4 Ways to Set Variables - Configu
January 17, 2025 - Environment variables in Java are external settings that influence system behaviors and configurations. They are useful for segregating environment-specific details from application code.
🌐
Medium
medium.com › nerd-for-tech › working-with-environment-variables-in-java-26b11f1344a5
Working with Environment Variables in Java | by Vonage Dev | Medium
March 24, 2024 - How It Works: Developers use environment variables like ENV_MODE to indicate the current execution context of the application, such as development, testing, staging, or production.
🌐
AutomationTestingHub
automationtestinghub.com › home › setup java environment variables
Setup Java Environment Variables - AutomationTestingHub
February 10, 2026 - To do this, you need to set up Java environment variables – primarily JAVA_HOME and PATH. In very simple terms, JAVA_HOME and PATH are pointers to the location where Java is installed, so you can run it from anywhere.
🌐
Twilio
twilio.com › en-us › blog › developers › community › working-with-environment-variables-in-java
Setting Environment Variables in Java | Twilio
June 11, 2025 - Environment variables are a great way to configure Java applications without having to explicitly store settings and secure credentials in code, such as for database and caching servers, or for third-party APIs.
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Environment_Variables.html
Environment Variables for Java Applications - PATH, CLASSPATH, JAVA_HOME
Many problems in the installation and running of Java applications are caused by incorrect setting of environment variables (global system variables available to all the processes/users running under the Operating System), in particular, PATH, CLASSPATH and JAVA_HOME.
🌐
TecAdmin
tecadmin.net › environment-variables-in-java
Using Environment Variables in Java – TecAdmin
April 26, 2025 - Some common use cases for environment variables include: ... Controlling the behavior of the application based on the current environment (development, staging, production) In Java, environment variables can be accessed using the System class.
🌐
Baeldung
baeldung.com › home › java › set an environment variable at runtime in java
Set an Environment Variable at Runtime in Java | Baeldung
June 20, 2024 - Our application can cache the variables on startup, and any further changes won’t have any effect. We don’t have this problem while changing the underlying Map using reflection, as it changes the value only on the Java side. Also, this approach would require a custom solution for different platforms. Because all OSs handle environment variables differently, the solution won’t be as cross-platform as the pure Java implementation.
🌐
iFour Technolab
ifourtechnolab.com › blog › java-and-environment-variables-a-brief-overview
Java And Environment Variables: A Brief Overview
May 5, 2022 - Environment variables help you configure Java applications without having to store settings in code outside where they need to be. Essentially, you can configure Java applications without jeopardizing the database or caching servers, which are often vital for third-party APIs.
🌐
Stack Abuse
stackabuse.com › managing-environment-variables-in-java
Managing Environment Variables in Java
September 15, 2023 - Their values are saved in memory, ... such as a, it's a lot easier to remember. Environment variables are a lot like usual programming variables, except that they're set somewhere outside the program....
🌐
Scaler
scaler.com › home › topics › how to get environment variable in java?
How to Get Environment Variable in Java? - Scaler Topics
March 31, 2024 - The very common way of getting the Environment variable in Java is by using the System class.
🌐
Reddit
reddit.com › r/java › set environment variable using java
r/java on Reddit: Set environment variable using Java
April 12, 2020 -

I'm sorry with mods. I haven't noticied that this subreddit aren't the right place to post my question. I'll remove the post as soon

Because I want to develop an application following 12-factor rules, I've encountered a problem managing the configuration part. So, after a search on Google, I've decided to write a library which load variables from env file in the system environment. But it seems that doesn't exist a simple method to set an environment variable. After a search, I've founded this tricky method, so my question is: exist a more clear method to set an environment variable in Java?

Top answer
1 of 3
2
I think you could set variables in two ways: 1.) using command line: java -Dvar1=value1 - Dvar2=value1... com.package.prog1 2.) using Docker to run your app and set variables like this
2 of 3
2
I think there is some confusion here. I have never heard of this 12-factor rules, I don't even agree with NOT checking in config files to the repository, there is no clear benefit stated in the article other than saying "don't check your config in your repository". Anyways back to the problem. You seem like what you wanted here is: create an env file start your program read the env file from your program and set the values as environment variables then read those env vars in other parts of the program? The article states exactly that env vars are language and os agnostic, so what would be the benefit of the java executable loading it's own environment variables from an env file? The article only states that your program should be able to read the configuration via environment variables which is System.getEnv("SOME_CONFIG_PARAM") This was easy. Now the second part of the problem is, how do you set the environment variable? It is OS specific, in linux it can be within a bash script: #!/bin/bash export SOME_CONFIG_PARAM=12 export SOME_OTHER_CONFIG_PARAM=B java -jar my-program.jar This is all how far the 12-factor article goes. Now is it usable? NO, it is not. modern frameworks like micronaut.io , or Eclipse Microprofile allows defining multiple configuration files in e.g. yaml format. you can create an application.yml with the default configuration, and e.g. application-test.yml for test configuration, and you can tell the framework which one to load based on a system property e.g. -Dmicronaut.environment=test which will merge these two configs, which you should check into your repository (except the secrets). Very flexible. Moreover you can still override the configuration values from environment variables following some simple rules. But if you are aiming something simple, then go with the above bash script approach. If you want an env file, you should write a bash script to read that env file and set every entry in it into the environment variables. This is a more flexible approach. Forget that you can solve everything within your java executable, there are some context that should be handled before running the application.
🌐
Medium
azimuahamed.medium.com › java-environment-variables-setup-windows-11-58fe71e43b5e
JAVA Environment Variables Setup Windows 11 | by Azim Uddin Ahamed | Medium
August 1, 2024 - For the “Variable value,” enter the path to your JDK installation directory. For example, C:\Program Files\Java\jdk-x.x.x (replace x.x.x with your actual version number). Click “OK” to save the variable.
🌐
Codefinity
codefinity.com › courses › v2 › fff6da48-3405-4507-a82d-4863fce92534 › f831fe4b-6f96-44d4-a4f0-b9580fb8d295 › 6499df25-e0f1-43b7-83e1-b14c5f91a343
Learn Accessing Environment Variables in Java | System Properties and Environment Variables
When you need to configure your Java applications dynamically, environment variables are a common solution. Java provides a straightforward way to access these variables using the System.getenv() method. This method returns a Map<String, String> of all environment variables, or you can retrieve a single variable by passing its name as an argument, such as System.getenv("PATH").