Skip to lesson

Environment variables, PATH and redirection

I'm on:

Showing steps for Windows. They are marked “On Windows” below.

An environment variable is a named setting, such as PATH or JAVA_HOME, that every program started from the terminal can read. PATH decides whether typing javac finds the compiler or fails with "not recognized".

Environment variables

An environment variable is a named piece of text that the computer keeps for programs to read, such as HOME (your home folder) or JAVA_HOME (the folder where the JDK is installed).

Programs often need to know where things are: your home folder, the JDK, a tool's settings. Instead of asking you each time, they read environment variables. You can read them too.

Example 1 · Read two environment variables

Windows PowerShell
PS C:\Users\Priya> $env:USERPROFILEPS C:\Users\Priya> $env:JAVA_HOMEOutputC:\Users\Priya
C:\Program Files\Java\jdk-19

What this code does

  • In PowerShell you read a variable with $env: before its name. USERPROFILE is your home folder on Windows.
  • JAVA_HOME prints the JDK folder. If it prints nothing, it is not set yet: the last section sets it.

A variable you set by typing a command lasts only until you close that window: $env:GREETING = "Hi" in PowerShell, export GREETING="Hi" on a Mac. Settings that should last are saved once, as the JAVA_HOME section shows.

PATH: how the terminal finds programs

PATH is the environment variable that lists the folders the shell searches, in order, for a program you type. The first folder that has a program with that name wins; if none has it, the command is not found.

When you type java, the shell does not search your whole disk. It checks only the folders in PATH, from the first to the last, as Install Java (opens in a new tab) showed.

On Windows

where.exe lists every PATH folder that has the program, in search order. The first line is the one that runs:

Windows PowerShell
PS C:\Users\Priya> where.exe javaC:\Program Files\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk-19\bin\java.exe
  • javapath is a small shortcut folder that Oracle's installer adds; it starts the JDK you installed. That is normal.
  • If java -version shows an old Java (such as 1.8), an old folder comes first in PATH: move the new JDK's bin folder above it in Environment Variables.
You type java -version. PATH lists three folders, checked from the top. C:\Windows\system32 has no java. C:\Program Files\Java\jdk1.8.0_401\bin has java, so Java 8 runs. C:\Program Files\Java\jdk-25\bin is never checked. Fix: move the jdk-25 folder above the old one.

Example 2 · Look inside PATH

Windows PowerShell
PS C:\Users\Priya> $env:Path -split ';'C:\Program Files\Common Files\Oracle\Java\javapath
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\WindowsPowerShell\v1.0
C:\Program Files\Java\jdk-19\bin
C:\Users\Priya\AppData\Local\Microsoft\WindowsApps

What this code does

  • On Windows, PATH is one long line with folders joined by ;. -split ';' shows one folder per line, in search order.
  • Your list will be longer: every tool you install may add a folder. Look for your JDK's bin folder in it.

"javac is not recognized" (or "command not found") means no PATH folder has javac: the JDK's bin folder is missing from PATH, or the window was opened before you changed it. A window reads PATH once, when it opens, so always open a new window after a change.

Redirection: save output with > and >>

Redirection sends a command's output into a file instead of the screen. The > sign writes the output into the file and replaces what was there; >> adds it to the end of the file.

Priya wants her marks in a file, one line at a time. echo normally prints to the screen; with > or >> after it, the text goes into a file instead.

marks.txt holds one line, Priya 86. echo "Ravi 58" > marks.txt replaces it: the file now holds only Ravi 58. echo "Ravi 58" >> marks.txt adds to the end instead: the file holds Priya 86 and then Ravi 58.

Example 3 · Build a file line by line

Windows PowerShell
PS C:\Users\Priya\college-projects> echo "Priya 86" > marks.txtPS C:\Users\Priya\college-projects> echo "Ravi 58" >> marks.txtPS C:\Users\Priya\college-projects> echo "Kabir 91" >> marks.txtPS C:\Users\Priya\college-projects> cat marks.txtOutputPriya 86
Ravi 58
Kabir 91

What this code does

  • The first line uses >, so marks.txt starts fresh with one line. The next two use >>, so each adds a line at the end. Only cat prints on the screen.
  • Do not create .java files with > in Windows PowerShell: it saves text in a format (UTF-16) that javac cannot read, and you get errors like unmappable character (0xFF) for encoding UTF-8. Write Java code in your editor.

It works for any command, including your Java programs: java MarksReport > report.txt saves everything the program prints. Careful: a single > on an existing file replaces it without asking.

JAVA_HOME

JAVA_HOME is an environment variable that holds the folder where the JDK is installed: the JDK folder itself, not its bin folder. Build tools such as Maven and Gradle, and some IDEs, read it to find Java.

Your javac already works through PATH. Set JAVA_HOME once now, so the build tools you meet later find Java on the first try.

On Windows

Use the same Environment Variables window as for PATH in Install Java. The value is the JDK folder, for example C:\Program Files\Java\jdk-25 (not ...\jdk-25\bin).

Windows PowerShell
PS C:\Users\Priya> $env:JAVA_HOMEC:\Program Files\Java\jdk-19
  1. Press the Windows key, type environment variables and open Edit the system environment variables. Click Environment Variables….
  2. Under System variables, click New….
  3. Variable name: JAVA_HOME. Variable value: your JDK folder. Click OK in every window.
  4. Open a new PowerShell window and check it with the command below.

Next step

Check your understanding

8 quick questions