🧭 How to Set Java PATH and CLASSPATH
Setting the PATH
and CLASSPATH
variables is essential for compiling and running Java programs from any directory. Here's how to configure them on different operating systems.
🪟 Windows
1. Set JAVA_HOME
-
Open Advanced System Settings
PressWin + S
, type Advanced System Settings, and click on View advanced system settings.
-
Environment Variables
In the System Properties window, click on the Environment Variables button.
-
Create JAVA_HOME Variable
Under System variables, click New.-
Variable name:
JAVA_HOME
-
Variable value: Path to your JDK folder (e.g.,
C:\Program Files\Java\jdk-17
)
-
2. Set PATH
-
Edit PATH Variable
In the System variables section, find and select the Path variable, then click Edit.
-
Add JDK bin Directory
Click New and add:
%JAVA_HOME%\bin
3. Set CLASSPATH
-
Create CLASSPATH Variable
Under System variables, click New.-
Variable name:
CLASSPATH
-
Variable value:
.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
(Oracle Docs)
-
🐧 Linux
1. Set JAVA_HOME
-
Edit Profile File
Open a terminal and edit your profile file:nano ~/.bashrc
-
Add JAVA_HOME Variable
At the end of the file, add:export JAVA_HOME=/usr/lib/jvm/java-17-openjdk export PATH=$JAVA_HOME/bin:$PATH
-
Apply Changes
Save and close the file, then apply the changes:source ~/.bashrc
2. Set CLASSPATH
-
Edit Profile File
Open a terminal and edit your profile file:nano ~/.bashrc
-
Add CLASSPATH Variable
At the end of the file, add:export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
-
Apply Changes
Save and close the file, then apply the changes:source ~/.bashrc
🍏 macOS
1. Set JAVA_HOME
-
Edit Profile File
Open a terminal and edit your profile file:nano ~/.zshrc
-
Add JAVA_HOME Variable
At the end of the file, add:export JAVA_HOME=$(/usr/libexec/java_home -v 17) export PATH=$JAVA_HOME/bin:$PATH
-
Apply Changes
Save and close the file, then apply the changes:source ~/.zshrc
2. Set CLASSPATH
-
Edit Profile File
Open a terminal and edit your profile file:nano ~/.zshrc
-
Add CLASSPATH Variable
At the end of the file, add:export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
-
Apply Changes
Save and close the file, then apply the changes:source ~/.zshrc
✅ Verify Installation
After setting the environment variables:
-
Open Command Prompt or Terminal
On Windows, open Command Prompt. On Linux/macOS, open Terminal. -
Check Java Version
Run the following command:java -version
You should see the installed Java version information.
No comments:
Post a Comment