Chapter 5: How to Set Up Java Path
- 4.1/5
- 9
- Mar 11, 2025
When working with Java, setting up the environment correctly is crucial to running Java programs. One of the key steps is setting the Java Path so that the system recognizes Java commands like javac and java globally.
Setting the Path variable ensures that Java commands work globally from any directory in the command prompt or terminal.
1. Open Command Prompt (cmd).
2. Find your Java installation path (e.g., C:\Program Files\Java\jdk-XX.X.X\bin).
3. Type the following command and press Enter:
Why Do We Need to Set the Java Path?
By default, Java binaries (like javac and java) reside in the JDK installation directory. Without setting the Java Path, you must manually navigate to the Java installation folder each time to run Java commands.Setting the Path variable ensures that Java commands work globally from any directory in the command prompt or terminal.
Setting Java Path in Windows
1) Temporary Path Setup (For Current Session Only)
This method sets the Java path temporarily for the current command prompt session.1. Open Command Prompt (cmd).
2. Find your Java installation path (e.g., C:\Program Files\Java\jdk-XX.X.X\bin).
3. Type the following command and press Enter:
set PATH=C:\Program Files\Java\jdk-XX.X.X\bin;%PATH%
4. Verify by running:
1. Find Java Installation Directory
- Open C:\Program Files\Java - Note the JDK version folder (e.g., jdk-XX.X.X)
2. Set Environment Variable - Open Control Panel → Search for Environment Variablesbr /> - Click on Edit the system environment variablesbr /> - Under System Properties, click Environment Variables
3. Update PATH Variable - Under System Variables, find Path and click Editbr /> - Click New, then paste the JDK bin path:
4. Verify Setup Open Command Prompt and check Java version:
Verify by running:
java -version javac -versionNote: This setting will be lost after closing the command prompt.
2) Permanent Path Setup (For All Sessions)
To permanently set the Java Path on Windows:1. Find Java Installation Directory
- Open C:\Program Files\Java - Note the JDK version folder (e.g., jdk-XX.X.X)
2. Set Environment Variable - Open Control Panel → Search for Environment Variablesbr /> - Click on Edit the system environment variablesbr /> - Under System Properties, click Environment Variables
3. Update PATH Variable - Under System Variables, find Path and click Editbr /> - Click New, then paste the JDK bin path:
C:\Program Files\Java\jdk-XX.X.X\bin- Click OK to save changes
4. Verify Setup Open Command Prompt and check Java version:
java -version javac -version
Setting Java Path in Linux OS
1) Temporary Path Setup (For Current Session Only)
Open the terminal and type:export PATH=/usr/lib/jvm/java-XX-openjdk-amd64/bin:$PATHReplace java-XX-openjdk-amd64 with your actual JDK installation path.
Verify by running:
java -version javac -versionNote: This setting will be lost after closing the terminal.
2) Permanent Path Setup
To make the Java Path persistent across reboots:1. Open the Terminal.
2. Edit the ~/.bashrc (for Bash shell) or ~/.bash_profile:
nano ~/.bashrc3. Add the following lines at the end:
export JAVA_HOME=/usr/lib/jvm/java-XX-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH4. Save and exit (CTRL + X, then Y and Enter).
5. Apply changes:
export JAVA_HOME=/usr/lib/jvm/java-XX-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATHVerify using:
java -version javac -versionFor Zsh users: Modify ~/.zshrc instead of ~/.bashrc.
Setting Java Path in macOS
1) Temporary Path Setup
To set the Java path temporarily, use:export PATH=/Library/Java/JavaVirtualMachines/jdk-XX.X.X.jdk/Contents/Home/bin:$PATHVerify:
java -version javac -versionThis setting will be lost after restarting the terminal.
2) Permanent Path Setup
To make it permanent:1. Open Terminal.
2. Edit the ~/.zshrc (for macOS Catalina and later) or ~/.bash_profile (for older versions):
nano ~/.zshrc3. Add these lines:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-XX.X.X.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATH4. Save and exit (CTRL + X, then Y and Enter).
5. Apply changes:
source ~/.zshrcVerify:
java -version javac -versionSetting the Java path correctly ensures that Java commands (javac, java) work seamlessly in Windows, Linux, and macOS. While temporary path setup works for a single session, configuring the path permanently saves time and effort.