How To Set Environment Variables In Java Code
- Details
- Written by
- Last Updated on 09 April 2022 | Print Email
Why practice you need to setup surroundings variables in Coffee programming?
Well, in this commodity I talked about the JVM, JRE and JDK, which are the cornerstones of the Java programming language. Y'all develop Coffee applications past using tools like editors, IDEs and servers. These tools need to use the Coffee compiler ( javac ) and Java launcher ( java ) to compile and run Java applications, but these tools don't know where the JRE or JDK is.
And so, past setting up the environment variables, you tell your tools that:
"Hey, you can notice the compiler and launcher hither and there".
The first thing you need to exercise afterward installing the JDK is creating an environment variable named JAVA_HOME and then update the PATH variable.
- JAVA_HOME : stores location of the JDK'due south installation directory. When you install development tools, they volition 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 Coffee evolution, you should update this environment variable by adding an entry to the bin directory under JDK'southward installation directory.
For case, if the JDK is installed at C:\Programme Files\Java\jdk1.8.0, so y'all need to ready:
- JAVA_HOME = C:\Program Files\Java\jdk1.8.0
- PATH = PATH + C:\Program Files\Java\jdk1.8.0\bin
Setting these environs variables on Windows is not hard. Just go to Control Console > System > Advanced system settings > Advanced > Surround Variables. Y'all can see the whole steps to setup this stuff in the post-obit commodity:
How to write, compile and run a hello globe Java program for beginners
Only that isn't cool, because I'thou about to show yous how to practice the same thing using command line prompt as shown below (Windows 7, 8 and x):
setx JAVA_HOME "C:\Program Files\Coffee\jdk1.viii.0" setx PATH "%PATH%;%JAVA_HOME%\bin";
The setx command permanently updates the surround variables. To add/update organization environment variables, you must use the -m switch and open the command prompt using Administrator privilege: Click Beginning, blazon cmd . When the cmd.exe icon appears, right click and select Run as ambassador.
To add/update system environment variables:
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -one thousand PATH "%PATH%;%JAVA_HOME%\bin";
I adopt 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 of import points:
- Always set up JAVA_HOME when preparing evolution environment for Java.
- JAVA_HOME points to the installation directory of JDK.
- The PATH variable should contain an entry pointing to JAVA_HOME\bin.
- Environs can exist prepare either via the GUI or control line prompt.
I promise that you have learned something new today. You tin as well picket the following video for visual steps:
Related Tutorials:
- How to prepare JAVA_HOME surround variable on Windows 10
- How to check Java version
- Understand 9 features of Java programming linguistic communication
- How to write, compile and run a hi world Java program for beginners
About the Writer:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the fourth dimension of Coffee 1.iv and has been falling in dear with Coffee since and then. Brand friend with him on Facebook and watch his Java videos you YouTube.
Add comment
Source: https://www.codejava.net/java-core/how-to-set-environment-variables-for-java-using-command-line

0 Response to "How To Set Environment Variables In Java Code"
Post a Comment