Environment variables


What is it?. An environment variable is like a global variable that's accessible to both the operating system and applications. Some are already defined, such as the variable called PATH. Others need to be defined by users. The one that's needed for Java is called CLASSPATH, which often needs to be defined or modified in both Mac-OS and Windows.

When do you deal with environment variables? You often need to either define some yourself or modify existing ones by adding something to them. Usually, environment variables contain strings that identify directories (folders), and often an environment variable will contain a group of such directories.

The PATH environment variable. This is typically what the operating system uses to find executables. It is especially useful when working at the command-line (Mac or Windows), because the command-line tool (Terminal on Mac, Command-prompt on Windows) will examine the PATH variable to discover where an executable can be found.

The CLASSPATH environment variable. This is used by Java (by both javac and java) to find other Java code or libraries that you invoke from your code.

Note to Windows users: You are either using one of the Linux options or using Windows-native (via Command-prompt or Powershell):

 


Topic 1: the PATH variable (Mac, Linux, Linux-on-Windows)

 

When does one need to set the PATH variable? Java typically comes installed with a Mac and so there's usually no need to set up this variable. Even when you install Java, the installation procedure will correctly set this up. However, you will need set this up if you install an older verson of Java.

Important: z-shell vs. bash on Mac OS-X, and Linux

In the instructions below, we'll assume .profile but you should substitute the appropriate initialization file.

How do you set the PATH variable? You do this by editing the file .profile (.zshrc if using z-shell) in the home directory.

 

Multiple strings inside PATH. Typically, a PATH variable contains many directories, each as a string. The strings are separated by a colon, as in:

  export PATH=$PATH:/Users/simha/Desktop/sillyapp:/Users/simha/Desktop/anotherapp/version3/bin/
  
Here, we've added the full directory as a string to each of two programs (perhaps programs we've written): So, when you type a command (program name) in Terminal, then Terminal will examine the different directories in PATH in order, and pick the first executable it finds to match the name.

This becomes an issue when there are two executables with the same name, as in:

  export PATH=$PATH:/Users/simha/Desktop/sillyapp1.0:/Users/simha/Desktop/sillyapp1.0:/Users/simha/Desktop/sillyapp2.0
  

Suppose both of these folders contain the program sillyprogram, as in:

Suppose, each of these contain an executable called sillyprogram. Then, when you type sillyprogram at the commandline, the Terminal will execute the first one above (version 1.0).

Thus, if you have TWO versions of Java, put the version you want executed first. You can do this as follows:

export PATH=/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home/bin:$PATH
  
Here, we're placing Java-10 ahead of the installed Java by appending the default (installed) contents of PATH to the folder for Java-10.
 

Use a period for the current directory. Typically, we write programs in a directory and we want the Terminal or Java to find these. The current directory is represented by a period (.) and this must be included. For example:

  export PATH=$PATH:/Users/simha/Desktop/sillyapp1.0:/Users/simha/Desktop/sillyapp1.0:/Users/simha/Desktop/sillyapp2.0:.
  
Notice the period all by itself, after a colon, which says "current directory".
 


Topic 2: the CLASSPATH variable (Mac, Linux, Linux-on-Windows)

 

Let's start by explaining what an executable jar file is. A plain jar file is just a zip file: a single file that rolls up a whole folder (including sub-folders). An executable jar file is one of two things: (1) a zip file that contains Java code and that can be executed directly without unpacking; or (2) a zip file that contains Java code that can be imported into your code. We'll try out both variations below, but will only typically need the second one.

Next, what is CLASSPATH? As mentioned before, an operating system maintains a few global variables that both the operating system and applications can access. CLASSPATH is a variable used by the Java compiler (which is called javac) and the Java interpreter (which is called java). The CLASSPATH variable tells the compiler where to find stuff. The variable holds a string that itself is a concatention of directory (folder) locations. There are two types of locations that one puts into the CLASSPATH string: (1) folders that contain Java code; (2) executable jar files. This is how the compiler knows where to get libraries, for example.
 

Let's start by adding a jar file to CLASSPATH.

 


Topic 3: CLASSPATH in Windows (Windows native)

 

There are two different complications with Windows:

 

Let's follow the above directions with CLASSPATH for Windows:

 


Topic 4: PATH in Windows and new installs of Java

 

Sometimes, when installing an older or newer version of Java, or installing a new program that you want to invoke in the Command-Prompt, you need to put the full directory path in the PATH variable.

For example, let's explain how to install a different version of Java, for example, Java-8.