Sub topic 5
Make Java Commands Available
You have installed a Java Development Kit, or JDK. Now you need to make its commands easy to find from a terminal.
A terminal is a window where you type commands for your computer to run. When you type java or javac, the terminal needs to know where to find those programs. This chapter shows how to give it that information on Windows, macOS, or Linux.
How the terminal finds a command
A directory is another name for a folder. The JDK has a directory named bin that contains its command programs:
javastarts the Java runtime, which runs Java programs.javacstarts the Java compiler, which turns Java source code into a form the runtime can use.
On Windows, these program files are usually named java.exe and javac.exe.
The terminal uses a setting named PATH to find programs. PATH is an environment variable: a named piece of information that programs can use. Its value is a list of directories. When you enter a command, the terminal searches those directories in order.
If the JDK’s bin directory is missing from PATH, the terminal may say that it cannot find java or javac. If an older Java installation appears earlier in PATH, the terminal may find that installation instead.
Changing your current working directory does not add the JDK to PATH. The working directory is the folder where your terminal is currently working.
Find your JDK’s bin directory
Before changing anything, find the JDK you installed. Open its installation directory in your computer’s file browser and look for a bin folder.
Check that bin contains both Java commands: java and javac, or java.exe and javac.exe on Windows.
Your JDK’s location depends on the installer and the version you chose. These are examples, not paths that work on every computer:
| System | Example JDK installation directory | Example `bin` directory |
|---|---|---|
| Windows | `C:\Program Files\Java\jdk-21` | `C:\Program Files\Java\jdk-21\bin` |
| macOS | `/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home` | `/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home/bin` |
| Linux | `/usr/lib/jvm/java-21-openjdk-amd64` | `/usr/lib/jvm/java-21-openjdk-amd64/bin` |
A path is the address of a file or directory. Write down the full path to your own JDK’s bin directory. You will use it in the instructions below.
Understand what your change affects
A terminal gets its environment variables when it starts. If you change a saved PATH setting, a terminal that was already open may keep its old value.
After making a saved change, close the terminal and open a new one. If you use a terminal inside another application, you may need to close and reopen that application too.
You may see another environment variable called JAVA_HOME. It usually points to the JDK installation directory, one level above bin. Some development tools ask for it, but it is not required just to make java and javac available through PATH. You do not need to set it for this chapter.
Windows: add the JDK to PATH
These steps use the environment variable settings in Windows 10 or 11. They change the setting for your Windows user account.
- Open the Start menu and search for
Edit environment variables for your account. - Open the matching result.
- Under User variables, look for a variable named
Path. - If
Pathexists, select it and choose Edit. - Choose New, then enter the full path to your JDK’s
bindirectory. For example, your entry might look likeC:\Program Files\Java\jdk-21\bin. Use your actual path, without quotation marks. - Select OK to save the change. Close the remaining settings windows with OK as needed.
- Close your terminal and open a new one.
If there is no Path under User variables, choose New in that section. Set the variable name to Path and its value to your JDK’s full bin path. Then save it and open a new terminal.
Windows separates Path entries with semicolons. In the usual Windows 10 or 11 editor, each entry has its own row, so you do not need to type a semicolon after the new row.
Windows also has a system Path, which applies to all users. System entries are normally searched before user entries. If an older Java command is in a system entry, adding your JDK to the user Path may not make the new one come first. You may need help from someone who can manage the computer’s system settings.
macOS and Linux: add the JDK to PATH
On macOS and Linux, a shell is the program in the terminal that reads your commands. Shells can read settings from a startup file, which is a text file used when a shell starts.
The instructions below cover two common shells: Zsh and Bash. First, find out which one is running. Enter:
echo "$0"The result often contains zsh or bash. A leading hyphen, as in -zsh, is fine. Follow the matching section below. If the result names a different shell, check that shell’s instructions for setting PATH.
If you use Zsh
Zsh commonly reads a file named .zshrc when an interactive terminal starts. On macOS, Zsh is a common default shell.
Open the file with the nano text editor:
nano ~/.zshrcThe ~ symbol means your home directory. If the file does not exist yet, nano can create it when you save.
Add a line like the following at the end of the file. This example uses a possible macOS JDK location. Replace the path inside the quotation marks, before :$PATH, with your own JDK’s bin path.
export PATH="/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home/bin:$PATH"On Linux, your path will look different. For example, it might begin with /usr/lib/jvm/.
In nano, press Ctrl+O to save, press Enter to confirm the file name, and then press Ctrl+X to exit. Close the terminal and open a new one.
If you use Bash
Bash commonly reads .bashrc for an interactive terminal that is not a login shell. A login shell is a shell started in a way that reads login startup files. To see which kind of Bash session you have, enter:
if shopt -q login_shell; then
echo "login shell"
else
echo "non-login shell"
fiRun that check in Bash, not Zsh.
For a non-login shell, open .bashrc:
nano ~/.bashrcFor a login shell, use the first of these files that already exists in your home directory: .bash_profile, .bash_login, or .profile. If none exists, you can create .bash_profile. For example, to open .bash_profile, enter:
nano ~/.bash_profileAdd a line like the following at the end of the file. This example uses a possible Linux JDK location. Replace the path before :$PATH with your own JDK’s bin path.
export PATH="/usr/lib/jvm/java-21-openjdk-amd64/bin:$PATH"Press Ctrl+O to save, press Enter to confirm the file name, and then press Ctrl+X to exit. Close the terminal and open a new one.
Some Linux systems do not have nano installed. If that command is not found, use another plain-text editor to edit the same startup file.
What the PATH line does
Consider the shape of the line you added:
exportmakes the setting available to programs started by the shell.- The path before the colon is your JDK’s
bindirectory. - The colon separates directories in a macOS or Linux
PATH. $PATHmeans the existing value ofPATH. Including it keeps the other directories.- The quotation marks keep the value together, including if the JDK path contains spaces.
The new directory comes first, so its Java commands can be found before commands in later directories.
Check which command locations are found
After opening a new terminal, you can check the command locations. This check is about finding the files; the next chapter covers checking their versions.
On Windows, enter these commands:
where.exe java.exe
where.exe javac.exeWindows may list more than one location. where.exe also checks the current directory, so its list is not always the same as the order in which PowerShell runs commands. In a normal setup, look for your JDK’s bin directory among the results.
On macOS or Linux, enter:
command -v java
command -v javacA result might be a path inside your JDK’s bin directory. On some systems it may instead be a path to a system-managed link. A link is a file that points to another file. The location check is useful, but it does not by itself prove which Java version will run.
If you get no location, or Windows reports that it could not find a file, check these points:
- Did you open a new terminal after saving the change?
- Did you enter the path to
bin, rather than only the JDK installation directory? - Does that
bindirectory contain bothjavaandjavac? - On macOS or Linux, did you save the line in the startup file for the shell you actually use?
- If another Java location appears first, could an earlier
PATHentry be taking priority?
You do not need to remove every other Java installation. The important thing is to know which commands your terminal will use. If you are unsure, keep your existing settings and use the version checks in the next chapter before making more changes.
Practice
Exercise
Your JDK is installed at C:\Program Files\Java\jdk-21. Which directory should you add to Windows Path?
Show answer
Add C:\Program Files\Java\jdk-21\bin. Use the actual JDK path on your computer if it differs.
Exercise
You saved a PATH change but an already-open terminal still cannot find javac. What should you try first?
Show answer
Close the terminal and open a new one so it can receive the saved setting. Then check that the JDK's bin directory is correct.
Exercise
In the shell line export PATH="/some/jdk/bin:$PATH", why keep :$PATH?
Show answer
It keeps the existing PATH directories after the new JDK directory, so the shell can still find other commands.