How to Add Javac to Path in Windows and Linux
-
Adding
javactoPATH: Simplifying Java Compilation -
Use Environment Variables (Windows) to Add
javactoPATH -
Use Terminal (Unix-Based Systems) to Add
javactoPATH -
Use Windows PowerShell to Add
javactoPATH - Conclusion
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:
- Convenience: Allows executing
javaccommands from any directory without specifying the full path to the compiler. - Ease of Development: Simplifies Java code compilation and development workflow.
- 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 onAdvanced system settings.
In the above image, click on
"edit the system environment variables"and clickOpenon the right side of it. -
A popup window will be opened named
System Properties. Click onEnvironment Variablesat the bottom of the window.
-
A new popup will open to set environment variables. Now go to the
System variablessection and choose theNewbutton to add a new system variable.
-
Give a new name as
JAVA_HOMEand give its value as the location where Java is kept, likeC:\Program Files\java\jdk1.8.0_191.
Simultaneously, we can browse the path using the
Browsebutton of the popup window. -
Search the
JAVA_HOMEvariable in theSystem variableslist. -
Go to the
Pathvariable and click on theEditbutton. A new popup will open. -
Click the
Newbutton on the new window opened.
-
Add
%JAVA_HOME%\binin the highlighted new line and click theOKbutton.
Other than those steps, there is another way to open the environment variables window.
-
Right-click on
My Computerover the desktop screen.
-
Click the
Propertiesat the very bottom of it. -
On the left pane, in the search bar, search
Advanced System Settings.
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_profileor~/.bashrcfile using a text editor likenanoorvi.nano ~/.bash_profile -
Add
javactoPATHby appending the JDKbindirectory to the existingPATHvariable.For Linux:
export PATH=$PATH:/usr/lib/jvm/java-xx-openjdk-amd64/binFor macOS: Edit the
~/.bash_profileor~/.bashrcfile.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_profileor.bashrc:source ~/.bash_profile -
Verify if
javacis added toPATHby executing:javac -versionIt 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
PATHVariable via PowerShellOpen PowerShell as an administrator. Right-click the
Startbutton and select"Windows PowerShell (Admin)"or"Terminal (Admin)"from the context menu.Execute the following command to add
javacto thePATHvariable:[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
javactoPATH, close and reopen PowerShell.Check if
javacis 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
javacto thePATHvariable in Windows by accessing the system’s environment variables. This method involved modifying thePathvariable to include the JDK’sbindirectory, enabling convenient access tojavacacross the system. - Unix-based Systems (Using Terminal): For Unix-based systems, the terminal was utilized to modify the
PATHvariable. The steps involved locating the Java installation directory and updating the~/.bash_profileor~/.bashrcfile with the appropriateexportcommand to addjavacto thePATH. - Windows (Using PowerShell): PowerShell was employed in Windows to update the
PATHvariable. This method involved running commands in PowerShell as an administrator to append the JDK’sbindirectory to thePATH, granting access tojavacfrom 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 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