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):
The setxcommand 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:
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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
I appreciate the article but I don't understand the purpose of showing the commands without the -m parameter. It's very confusing to list the commands twice.
It says WARNING: The data being saved is truncated to 1024 characters.
That content of the PATH is too long. You should find and delete unused sub paths.
I am coding a batch file that sets the JAVA_HOME, MAVEN_HOME and respective Path in a remote system. I cannot find and delete any unused path from another system.
Comments
WARNING!!! WIN 10
This command clean path config and update for this command.
Vary bad command.
I am coding a batch file that sets the JAVA_HOME, MAVEN_HOME and respective Path in a remote system. I cannot find and delete any unused path from another system.
That content of the PATH is too long. You should find and delete unused sub paths.