How to Add Javac to Path in Windows and Linux

Rashmi Patidar Feb 12, 2024
  1. Adding javac to PATH: Simplifying Java Compilation
  2. Use Environment Variables (Windows) to Add javac to PATH
  3. Use Terminal (Unix-Based Systems) to Add javac to PATH
  4. Use Windows PowerShell to Add javac to PATH
  5. Conclusion
How to Add Javac to Path in Windows and Linux

Environment variables are the dynamic and run-time values that the OS and other software residing over your system can use to determine the information related to the system.

Adding javac to the system’s PATH variable is essential for executing Java compiler commands from any directory in the command prompt or terminal. It enables easy access to Java tools and facilitates the compilation of Java source files without specifying the full path to the compiler.

Below is a comprehensive article detailing various methods to accomplish this across different operating systems.

Adding javac to PATH: Simplifying Java Compilation

The Java Development Kit (JDK) comes with a powerful tool, javac, used to compile Java source code into bytecode. It’s crucial to add its location to the system’s PATH environment variable to utilize javac conveniently from any directory.

Adding javac to PATH offers several advantages:

  1. Convenience: Allows executing javac commands from any directory without specifying the full path to the compiler.
  2. Ease of Development: Simplifies Java code compilation and development workflow.
  3. Flexibility: Enables seamless integration with development environments and build tools.

This article explores multiple methods across different operating systems to accomplish this task.

Use Environment Variables (Windows) to Add javac to PATH

There are two types of environment variables: user environment variables and system environment variables.

The User Environment Variables store information related to the user. As the name suggests, it stores details of a user after the user gets logged in.

The System Environment Variables are a step beyond user variables; they hold the information related to the system and are not straight away related to a user. It stores the path variables and details that an application needs in the course of execution.

The steps to add javac to the path variable in Windows 10 are below:

  • Search "edit system variables" in the search bar of Windows 10 or "about your pc" in Windows 11, then click on Advanced system settings.

    Search environment variables in search bar

    In the above image, click on "edit the system environment variables" and click Open on the right side of it.

  • A popup window will be opened named System Properties. Click on Environment Variables at the bottom of the window.

    System properties popup

  • A new popup will open to set environment variables. Now go to the System variables section and choose the New button to add a new system variable.

    Environment variables popup

  • Give a new name as JAVA_HOME and give its value as the location where Java is kept, like C:\Program Files\java\jdk1.8.0_191.

    set java home to add new system variable

    Simultaneously, we can browse the path using the Browse button of the popup window.

  • Search the JAVA_HOME variable in the System variables list.
  • Go to the Path variable and click on the Edit button. A new popup will open.
  • Click the New button on the new window opened.

    Edit path variable

  • Add %JAVA_HOME%\bin in the highlighted new line and click the OK button.

    Add java home in Path variable

Other than those steps, there is another way to open the environment variables window.

  • Right-click on My Computer over the desktop screen.

    Go to Properties using My computer

  • Click the Properties at the very bottom of it.
  • On the left pane, in the search bar, search Advanced System Settings.

    Search environment properties in properties pane

Then, continue with the steps above.

Use Terminal (Unix-Based Systems) to Add javac to PATH

The PATH environment variable in Unix-based systems determines where the system looks for executable files when a command is executed in the terminal. Adding javac to PATH enables the system to locate the Java compiler from any directory, simplifying Java development tasks.

Here’s a guide on how to achieve this through the terminal:

  • Locate Java Installation Directory

    Find the directory where Java is installed and copy it. The path is usually /usr/lib/jvm/java-xx-openjdk-amd64/bin.

  • Update Bash Profile

    Open the terminal and edit the ~/.bash_profile or ~/.bashrc file using a text editor like nano or vi.

    nano ~/.bash_profile
    
  • Add javac to PATH by appending the JDK bin directory to the existing PATH variable.

    For Linux:

    export PATH=$PATH:/usr/lib/jvm/java-xx-openjdk-amd64/bin
    

    For macOS: Edit the ~/.bash_profile or ~/.bashrc file.

    export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk1.x.x_xx/bin
    
  • Save the changes and exit the editor.

    Press on Ctrl+X, then Y to confirm, and press Enter. If using ~/.bashrc, repeat the same steps in that file.

  • Apply Changes

    To make the changes effective, either restart the terminal or execute the following command to reload the updated .bash_profile or .bashrc:

    source ~/.bash_profile
    
  • Verify if javac is added to PATH by executing:
    javac -version
    

    It should display the installed Java compiler’s version information.

After successfully adding javac to PATH, compiling Java source files becomes more straightforward. Adding javac to the PATH variable in Unix-based systems simplifies Java development tasks by allowing the execution of javac commands from any directory.

This streamlines the workflow, enhances convenience, and facilitates a smoother Java development experience.

Use Windows PowerShell to Add javac to PATH

In the Windows operating system, the PATH variable stores a list of directories where the system looks for executable files when a command is executed in PowerShell. Adding javac to the PATH enables users to execute Java compiler commands from any directory within PowerShell without specifying the full path to the compiler.

Below are the steps to achieve this using PowerShell:

  • Locate Java Installation Directory

    Identify the directory where the Java Development Kit (JDK) is installed and copy it. The path commonly appears as C:\Program Files\Java\jdk1.x.x_xx.

  • Update PATH Variable via PowerShell

    Open PowerShell as an administrator. Right-click the Start button and select "Windows PowerShell (Admin)" or "Terminal (Admin)" from the context menu.

    Execute the following command to add javac to the PATH variable:

    [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Program Files\Java\jdk1.x.x_xx\bin", "User")
    

Replace C:\Program Files\Java\jdk1.x.x_xx\bin with the actual path to your JDK’s bin directory.

  • Verify Changes

    To confirm the successful addition of javac to PATH, close and reopen PowerShell.

    Check if javac is accessible by executing the following command:

    javac -version
    

It should display the installed Java compiler’s version information.

After adding javac to PATH, compiling Java source files becomes more convenient. Adding javac to the PATH variable in Windows via PowerShell simplifies Java development tasks by providing easy access to the Java compiler from PowerShell directories.

This enhances workflow efficiency and convenience and supports a smoother Java development experience in the Windows environment.

Conclusion

The article discussed multiple methods to accomplish this task across different operating systems, outlining their benefits and providing step-by-step instructions.

  • Windows (Using Environment Variables): The guide detailed the steps to add javac to the PATH variable in Windows by accessing the system’s environment variables. This method involved modifying the Path variable to include the JDK’s bin directory, enabling convenient access to javac across the system.
  • Unix-based Systems (Using Terminal): For Unix-based systems, the terminal was utilized to modify the PATH variable. The steps involved locating the Java installation directory and updating the ~/.bash_profile or ~/.bashrc file with the appropriate export command to add javac to the PATH.
  • Windows (Using PowerShell): PowerShell was employed in Windows to update the PATH variable. This method involved running commands in PowerShell as an administrator to append the JDK’s bin directory to the PATH, granting access to javac from any PowerShell directory.

Overall, whether in Windows or Unix-based systems, adding javac to the PATH variable simplifies the execution of Java compiler commands, contributing to a more efficient and user-friendly development experience.

Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Javac