How do you set environment variables?
What are Environment Variables?Environment variables are global system variables accessible by all the processes/users running under the Operating System (OS), such as Windows, macOS and Linux. Environment variables are useful to store system-wide values, for examples, Show
(Windows) Environment VariablesEnvironment Variables in Windows are NOT case-sensitive (because the legacy DOS is NOT case-sensitive). They are typically named in uppercase, with words joined with underscore ( Display Environment Variables and their ValuesTo list ALL the environment variables and their values, start a CMD and issue the command " set
COMPUTERNAME=xxxxxxx
OS=xxxxxxx
PATH=xxxxxxx
....... Try issuing a " To display a particular variable, use command " set COMPUTERNAME COMPUTERNAME=xxxxxx echo %COMPUTERNAME% COMPUTERNAME=xxxxxx Set/Unset/Change an Environment Variable for the "Current" CMD SessionTo set (or
change) a environment variable, use command "
For examples, set MY_VAR=hello set MY_VAR MY_VAR=hello set MY_VAR= set MY_VAR Environment variable MY_VAR not defined An environment variable set via the " Using an Environment VariableTo reference a variable in Windows, use echo %PATH% PATH=xxxxxxx set PATH=c:\myBin;%PATH% PATH=c:\myBin;[existing entries] How to Add or Change an Environment Variable "Permanently"To add/change an environment variable permanently in Windows (so that it is available to ALL the Windows' processes/users and stayed across boots):
You need to RE-START CMD for the new setting to take effect! To verify the new setting, launch CMD: set VAR_NAME
VAR_NAME=VAR_VALUE PATH Environment Variable in Windows When you launch an executable program (with file extension of " abc 'abc' is not recognized as an internal or external command, operable program or batch file. abc Bad command or file name To list the current PATH
PATH=path2;path2;path3;... How to Add a Directory to the PATH in WindowsTo add a directory to the existing
You need to RE-START CMD for the new PATH setting to take effect! To verify the new setting, launch CMD: PATH
PATH=path2;path2;path3;... Notes:
(macOS/Linux) Environment VariablesEnvironment variables in macOS/Unixes are case-sensitive. Global environment variables (available to ALL processes) are named in uppercase, with words joined with underscore ( Using Environment Variables in Bash ShellMost of the Unixes
(Ubuntu/macOS) use the so-called Bash shell. Under
How to Set an Environment Variable Permanently in Bash ShellYou can set an environment variable permanently by placing an For example, to add a directory to the export PATH=/usr/local/mysql/bin:$PATH (For Java) You can set the export CLASSPATH=.:/usr/local/tomcat/lib/servlet-api.jar Take note that Bash shell uses colon ( To refresh the
bash shell, issue a " source ~/.bashrc source ~/.bash_profile source ~/.profile source /etc/profile (Notes) For the older csh (C-shell) and ksh (Korn-shell)
PATH Environment VariableMost of
the Unixes and macOS use the so-called Bash Shell in the "Terminal". When you launch an executable program (with file permission of executable) in a Bash shell, the system searches the program in ALL the directories listed in the abc
bash: abc: command not found Take note that the current directory ( ./myProgram To list the current echo $PATH
path2:path2:path3:... How to Add a Directory to the PATH in macOS/LinuxTo add a directory to the existing export PATH=/path/to/dir:$PATH To refresh
the bash shell, issue a " source ~/.bashrc source ~/.bash_profile source ~/.profile source /etc/profile To verify the new setting, launch CMD: echo $PATH
path2:path2:path3:... Notes:
Java Applications and the Environment Variables PATH, CLASSPATH, JAVA_HOMEMany 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, PATHWhen you launch a program from the command line, the Operating System uses the PATH (For Windows) When you launch an executable program (with file extension of " abc 'abc' is not recognized as an internal or external command, operable program or batch file. abc Bad command or file name For example, if you are trying to use Java Compiler " javac Hello.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
For Java applications,
The JDK's " To add a directory (say JDK's " PATH (For macOS/Linux)Most of the Unixes and macOS use the so-called Bash Shell in the "Terminal". When you launch an executable program (with file permission of executable) in a Bash shell, the system searches the program in ALL the directories listed in the abc
bash: abc: command not found For example, if you are trying to use Java Compiler " javac Hello.java
bash: javac: command not found To support Java applications, you need to include the JDK's " CLASSPATHJava Archive (JAR) FileFor ease of distribution, Java classes are often archived (zipped) together into a so-called JAR file. To use a third-party Java package, you need to place the distributed JAR file in a location that is available to the Java Compiler and Java Runtime. How Classes are Found?Java Compiler ("
Cannot Find ClassesIf the Java Runtime (" Similarly, Java Compiler (" Notes: External native libraries (" CLASSPATH Environment VariableThe A common problem in running hello-world program is: For a beginner, no explicit The Read JDK documents "Setting the CLASSPATH Environment Variable (For Windows)The Example: Displaying and changing set CLASSPATH set CLASSPATH=.;d:\tomcat\lib\servlet-api.jar You can set the CLASSPATH (for macOS/Ubuntu)
JAVA_HOME and JRE_HOMEMany Java applications (such as Tomcat) require the environment variable How to Set JAVA_HOME in WindowsFirst, check if set JAVA_HOME If To set/change
You need to RE-START CMD for the new setting to take effect! To verify the new setting, re-start CMD: set JAVA_HOME
JAVA_NAME=C:\Program Files\Java\jdk-13.0.1 How to Set JAVA_HOME in Linux/macOS (Bash Shell)First, check if echo $JAVA_HOME [TODO] find macOS and Ubuntu JDK installed directory. Add the the following line at the end of [TODO] How to un-hide for macOS/Ubuntu. export JAVA_HOME=/path/to/JDK-installed-directory You need to refresh the bash shell for the new settings to take effect.
Issue a " source ~/.bashrc echo $JAVA_HOME Windows vs. Unixes/macOSJava is platform independent. Java classes run in Windows as well as Unixes - binary compatible.
|