Sub topic 5
Make Java Commands Available
When you type java or javac in a terminal, your computer needs to find the matching program. It looks in folders listed in PATH. PATH is a setting that tells the terminal where to look for commands.
The Java Development Kit (JDK) contains both commands in its bin folder. Adding that folder to PATH lets you use the commands without typing their full locations.
Find the JDK’s `bin` folder
First, find the JDK folder you installed in the previous chapter. Open it and look for a folder named bin.
Inside bin, look for java and javac. On Windows, these files are named java.exe and javac.exe. Copy or note the full path to the bin folder, not just the path to the JDK folder above it.
For example, a Windows path might look like C:\Program Files\Eclipse Adoptium\jdk-21.0.6.7-hotspot\bin. Your folder name may be different.
On Windows
You can add the JDK folder to your user PATH without changing the PATH for every user on the computer.
- Open the Start menu and search for Edit environment variables for your account. An environment variable is a named setting that programs can use.
- Open it. In the User variables section, select Path, then select Edit.
- Select New and paste the full path to your JDK’s
binfolder. Do not add quotes around it. - Select OK in each open settings window.
- Close and reopen your terminal so it receives the updated setting.
If there is no Path under User variables, select New there. Set the variable name to Path and its value to the full path to the JDK’s bin folder.
On macOS
Many macOS JDK installers register the JDK with a tool called java_home. The command below uses that tool to find a JDK and adds its bin folder to PATH.
- Open your terminal.
- Run this command:
export PATH="$(/usr/libexec/java_home)/bin:$PATH"The export command changes the setting for this terminal. The colon separates the new folder from the folders already in PATH. The "$PATH" part keeps those existing folders.
To make the change available in future terminal sessions, add the same line to a startup file. A startup file is a file your shell reads when it starts. A shell is the program that reads your terminal commands.
For the usual macOS shell, Zsh, open its startup file:
nano ~/.zshrcnano is a terminal text editor. Add the export line on a new line. Press Ctrl+O, then Enter to save. Press Ctrl+X to exit. Close and reopen the terminal.
If you use Bash instead of Zsh, add the line to ~/.bash_profile instead. java_home may choose a different installed JDK if you have more than one; the next chapter shows how to check the version.
On Linux
Your Linux installer may already have made the commands available. You can check with the commands in the next section before changing PATH.
If you need to add the JDK yourself, use this example. It assumes your JDK folder is named jdk inside your home folder. Your home folder is your personal folder on the computer.
export PATH="$HOME/jdk/bin:$PATH"If your JDK is elsewhere, replace $HOME/jdk/bin with the actual path to its bin folder. Keep the quotes and :$PATH so paths with spaces work and your existing entries remain available.
The change above lasts only for the current terminal. To keep it for future sessions, add the same line, with your actual JDK path, to your shell’s startup file. For Bash, open:
nano ~/.bashrcAdd the line, then press Ctrl+O, Enter, and Ctrl+X to save and exit. For Zsh, use ~/.zshrc instead. Close and reopen the terminal. If your Bash terminal does not load .bashrc automatically, you can load it with source ~/.bashrc.
Check which commands can be found
On Windows, run:
where.exe java
where.exe javacOn macOS or Linux, run:
command -v java
command -v javacThese commands show locations where the Java commands can be found. A missing result means the corresponding command was not found in the search locations. Check that you added the actual JDK bin folder, then open a new terminal and try again.
If you have more than one JDK, an older one may be found first. On Windows, system PATH entries usually come before user PATH entries, so adding a user entry may not override an older system entry. Changing a system entry may require an administrator. On macOS and Linux, placing the JDK folder before $PATH, as shown above, gives it priority among PATH entries.
Finding the commands does not yet confirm their versions or that both work. You will check those in the next chapter.
Exercise
You installed a JDK, but your terminal cannot find javac. Which folder should you add to PATH, and what should you do afterward?
Show answer
Add the installed JDK’s bin folder without removing existing PATH entries. Open a new terminal, then check whether javac can be found.