How to Set the Path in Java: A Complete Guide
Learn the importance of setting the path for Java development to effectively use tools like javac
and java
. This comprehensive guide outlines the reasons for setting the path and explores two essential methods for configuring the Java path, ensuring smooth compilation and execution of your Java programs.
How to Set Path in Java
Why Set the Path?
The path is required for using tools like javac
and java
. If your Java files are outside the JDK/bin directory, you need to set the path.
Ways to Set the Path
There are two ways to set the path in Java:
- Temporary
- Permanent
Setting Temporary Path of JDK in Windows
- Open the command prompt.
- Copy the path of the JDK/bin directory.
- Type in command prompt:
set path=copied_path
Example: set path=C:\Program Files\Java\jdk1.6.0_23\bin
Setting Permanent Path of JDK in Windows
- Go to MyComputer properties.
- Click on the advanced tab.
- Click on environment variables.
- Click on the new tab of user variables.
- Write
path
in the variable name. - Copy the path of the bin folder.
- Paste the path of the bin folder in the variable value.
- Click on OK.
Now your permanent path is set, and you can execute any Java program from any drive.
Setting Java Path in Linux OS
Setting the path in Linux OS is similar to Windows but uses the export
tool.
Example: export PATH=$PATH:/home/jdk1.6.01/bin/
Here, we have installed the JDK in the home directory under Root (/home).
Java Install
Some PCs might have Java already installed.
To check if you have Java installed on Windows:
Example: List installed packages
C:\Users\Your Name>java -version
If installed, you will see:
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
If not, download it for free at oracle.com.
Note: You can write Java in a text editor or an IDE like IntelliJ IDEA, Netbeans, or Eclipse.
Setup for Windows
- Go to "System Properties" (Control Panel > System and Security > System > Advanced System Settings)
- Click on "Environment variables" under the "Advanced" tab
- Select the "Path" variable in System variables and click "Edit"
- Click "New" and add the path where Java is installed, followed by \bin (e.g., C:\Program Files\Java\jdk-11.0.1\bin)
- Click "OK" and save the settings
- Open Command Prompt (cmd.exe) and type
C:\Users\Your Name>java -version
If installed successfully, you will see:
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
Java Quickstart
In Java, every application begins with a class name that matches the filename.
Let's create a file called Main.java with the following code:
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to Java");
}
}
Save the code as "Main.java". Open Command Prompt, navigate to the directory where you saved your file, and type:
C:\Users\Your Name>javac Main.java
This will compile your code. If there are no errors, type:
C:\Users\Your Name>java Main
The output should be:
Output
Welcome to Java