Environment variables, PATH and redirection
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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.USERPROFILEis your home folder on Windows. JAVA_HOMEprints 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:
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
javapathis a small shortcut folder that Oracle's installer adds; it starts the JDK you installed. That is normal.- If
java -versionshows an old Java (such as1.8), an old folder comes first in PATH: move the new JDK'sbinfolder above it in Environment Variables.
Example 2 · Look inside PATH
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
binfolder 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.
Example 3 · Build a file line by line
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
>, somarks.txtstarts fresh with one line. The next two use>>, so each adds a line at the end. Onlycatprints on the screen. - Do not create
.javafiles with>in Windows PowerShell: it saves text in a format (UTF-16) thatjavaccannot read, and you get errors likeunmappable 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).
PS C:\Users\Priya> $env:JAVA_HOMEC:\Program Files\Java\jdk-19
- Press the Windows key, type
environment variablesand open Edit the system environment variables. Click Environment Variables…. - Under System variables, click New….
- Variable name:
JAVA_HOME. Variable value: your JDK folder. Click OK in every window. - Open a new PowerShell window and check it with the command below.
Next step
Check your understanding
8 quick questions