How to Set Java PATH and CLASSPATH on Windows, Linux, and macOS (Step-by-Step Guide)

🧭 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

  1. Open Advanced System Settings
    Press Win + S, type Advanced System Settings, and click on View advanced system settings.





  2. Environment Variables
    In the System Properties window, click on the Environment Variables button.



  3. 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

  1. Edit PATH Variable
    In the System variables section, find and select the Path variable, then click Edit.



  2. Add JDK bin Directory
    Click New and add:
    %JAVA_HOME%\bin



3. Set CLASSPATH

  1. Create CLASSPATH Variable
    Under System variables, click New.

    • Variable name: CLASSPATH

    • Variable value: .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar(Oracle Docs)




Do you want to learn and see the steps to set PATH and CLASS PATH in a video tutorial, Watch video: 
                   



🐧 Linux

1. Set JAVA_HOME

  1. Edit Profile File
    Open a terminal and edit your profile file:

    nano ~/.bashrc
    

  1. 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
    

  1. Apply Changes
    Save and close the file, then apply the changes:

    source ~/.bashrc
    

2. Set CLASSPATH

  1. Edit Profile File
    Open a terminal and edit your profile file:

    nano ~/.bashrc
    

  1. Add CLASSPATH Variable
    At the end of the file, add:

    export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
    

  1. Apply Changes
    Save and close the file, then apply the changes:

    source ~/.bashrc
    


🍏 macOS

1. Set JAVA_HOME

  1. Edit Profile File
    Open a terminal and edit your profile file:

    nano ~/.zshrc
    

  1. 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
    

  1. Apply Changes
    Save and close the file, then apply the changes:

    source ~/.zshrc
    

2. Set CLASSPATH

  1. Edit Profile File
    Open a terminal and edit your profile file:

    nano ~/.zshrc
    

  1. Add CLASSPATH Variable
    At the end of the file, add:

    export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
    

  1. Apply Changes
    Save and close the file, then apply the changes:

    source ~/.zshrc
    


✅ Verify Installation

After setting the environment variables:

  1. Open Command Prompt or Terminal
    On Windows, open Command Prompt. On Linux/macOS, open Terminal.

  2. Check Java Version
    Run the following command:

    java -version
    

You should see the installed Java version information.



No comments:

Post a Comment

Featured Post

Add Keyboard Shortcuts to Tkinter GUI – Python Hotkeys with Bind Examples

  ← Back to Home ⌨️ Bonus Part 5: Keyboard Shortcuts & Hotkeys in Tkinter – Boost App Productivity 📘 Overview Keyboard shortcuts ...

Popular Posts