How to Add Javac to Path in Windows and Linux
-
Adding
javac
toPATH
: Simplifying Java Compilation -
Use Environment Variables (Windows) to Add
javac
toPATH
-
Use Terminal (Unix-Based Systems) to Add
javac
toPATH
-
Use Windows PowerShell to Add
javac
toPATH
- 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
javac
commands 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 clickOpen
on the right side of it. -
A popup window will be opened named
System Properties
. Click onEnvironment Variables
at the bottom of the window. -
A new popup will open to set environment variables. Now go to the
System variables
section and choose theNew
button to add a new system variable. -
Give a new name as
JAVA_HOME
and 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
Browse
button of the popup window. -
Search the
JAVA_HOME
variable in theSystem variables
list. -
Go to the
Path
variable and click on theEdit
button. A new popup will open. -
Click the
New
button on the new window opened. -
Add
%JAVA_HOME%\bin
in the highlighted new line and click theOK
button.
Other than those steps, there is another way to open the environment variables window.
-
Right-click on
My Computer
over the desktop screen. -
Click the
Properties
at 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_profile
or~/.bashrc
file using a text editor likenano
orvi
.nano ~/.bash_profile
-
Add
javac
toPATH
by appending the JDKbin
directory to the existingPATH
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 toPATH
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 PowerShellOpen 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 thePATH
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
toPATH
, 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 thePATH
variable in Windows by accessing the system’s environment variables. This method involved modifying thePath
variable to include the JDK’sbin
directory, enabling convenient access tojavac
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 appropriateexport
command to addjavac
to thePATH
. - 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’sbin
directory to thePATH
, granting access tojavac
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 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