Monday, August 9, 2021

How to Set Path for Java Unix Linux and Windows

PATH is one of the fundamental Environment variables on shell or DOS but it’s commonly associated with Java mainly because if we try to run a java program that doesn't include Java executable in PATH then we say PATH is not set for Java and we need to set the path for Java. I have also seen the developer getting confused over path and classpath in java. Though both path and classpath provide run-time settings for any java environment which is required to compile and executes Java programs they are completely different from each other.


Classpath is usually used to find out classes and is mostly associated with the lib part while PATH is used to find the executable or command to be executed. In order to compile and run a java program from the command line, your PATH environment variable must have "javac" and "java" on it. 

In this Java PATH tutorial, we will see what is PATH for Java, How to Set a Path for Java, and how to troubleshoot PATH related issues.


What is Path in Java

First of all, PATH is not specific to java it’s a shell concept and also available in Windows and DOS. It’s represented by an Environment variable called "PATH" and that's why it’s known as path. 

Whenever you type a command in the shell in UNIX or Linux or in command prompt in the windows machine, the command will be looked on PATH and if the shell is not able to find the command in PATH it says "not recognized" or the wrong command. 

Now for compiling and running, we use two java commands "javac" and "java" and these commands don't come by default with Windows or Unix instead they come when you install JDK in your machine. 

Now to successfully compile and run a java program in either windows or Linux you must have these two commands or executable in your PATH environment variable and that is called Setting Path for Java.


Setting Path for Java in Unix/Linux and Windows

Now, let's see how you can set the PATh for Java in a Linux or UNIX based system, along with Windows for your development environment. 

1. How to check if "java" or "javac" is in PATH

Javac and Java command resides under /bin directory or your Java installation directory. In my machine its "C:\Program Files\Java\jdk1.6.0_26\bin"

If you have this bin directory in path it means java and javac will be in path and Path is set to run Java Program. There are two ways you verify whether java is in path or not.

1) Simple and easy way
Open a command prompt window by pressing start -->run-->cmd and then typing "java" or "javac" in command prompt as shown in below example

C:\Documents and Settings>java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
    -client       to select the "client" VM
    -server       to select the "server" VM
    -hotspot      is a synonym for the "client" VM [deprecated]



How to set Path for Java in Unix, windows and Linux


If it displays a lot of output means Java is in your path and similarly you can check for "javac”, on the other hand, if java is not in your system's path you will get below output in UNIX

stock_trader$ javac
javac: not found

and in windows
C:\Documents and Settings>javac
'javac' is not recognized as an internal or external command,
operable program or batch file.

If Java or Javac is not in your path then you can add them into path by adding "bin" directory or your JDK installation directory into environment variable "PATH". Both windows and UNIX use same name.

How to set PATH for Java in windows

I say pretty easy just add bin directory of your JDK installation directory into PATH environment variable. You can do this either from command prompt or by using windows advanced environment editor

1) Setting Java PATH using command prompt in windows


Use "set" command to set value of PATH environment variable as shown in below example:

C:\Documents and Settings>set PATH=%PATH%; C:\Program Files\Java\jdk1.6.0_26\bin

%PATH% is actually representing the current path and we are appending java bin directory into PATH. Note that every path in windows is comma (;) separated while in UNIX it would be colon (:) separated


2) Setting Java PATH using windows environment variable editor


Use short cut "window key + pause/break" --> Advanced --> Environment Variables -->PATH

Just append the path of java installation directory here and you are done. Open a command prompt and type java or javac and you can see the output.


How to set Java PATH in UNIX or Linux

Setting the PATH for java in UNIX is similar to the way we did for it on windows using command prompt. In UNIX just open any shell and execute below command

set PATH=${PATH}:/home/opt/jdk1.6.0_26/bin

Remember here each element in PATH is colon-separated.


Related Java tutorials

9 comments :

Anonymous said...

How is it different to set PATH in Windows XP and Windows 7 ? What will happen if we don't set PATH is there any default PATH in Java ? How can we override PATH in Java ? Also many times we need to set PATH from Command Prompt can you also let us know how to set PATH from Command Prompt in DOS, Windows and Linux ?

Reema said...

I am getting java.lang.unsatisfiedlinkerror no ocijdbc10 in java.library.path , does it anything to with path setting in Java. I have already set PATH for java in environment variable PATH , still I am getting this error, please help.

Jessy said...

I don't think any difference in PATH setting for windows XP and windows 7 , you can set PATH either from command prompt or windows GUI. PATH is a operating system variable where it look for commands and executable. when you run java program you execute "java" command whose binary lies in JAVA_HOME/bin directory , if that directory is not included in PATH than Operating System will not find it and throw "not recognized as internal or external command"

Anonymous said...

i searched many articles especially about the path , than god , i found this excellent article
with very helpfull explanation.

Anonymous said...

In fact java.exe and javaw.exe are copied into your %systemroot% under windows on jre installation,
you won't see any additional path entries while java will be available from command line this way

Unknown said...

It didn't help me with my problem
😔

Unknown said...

It didn't help me with my problem

javin paul said...

Hello @Unknown, can you elaborate about your problem and what did you try, we may be able to help?

Anji Battula said...

Step1 -> Go to 'system'
step2 -> Go to advanced settings
step3 -> click on envirment Variables
step4 -> click on edit
step5 -> click on new and browse the jdk path "C:\Program Files\Java\jdk1.6.0_26\bin"

They try it will work

Post a Comment