Senin, 10 Maret 2014

How to Set Path for Java Unix Linux and Windows




PATH is one of fundamental Environment variable on shell or DOS but it’s commonly associated with Java mainly because if we try to run a java program which doesn't include Java executable in PATH then we say PATH is not set for Java and we need to set path for Java. I have also seen developer getting confused over path and classpath in java. Though both path and classpath provides run-time settings for any java environment which is required to compile and execute Java program they are completely different to each other. Classpath is usually used to find out classes and mostly associated with lib part while PATH is used to find the executable or command to be executed. In order to compile and run java program from 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 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 Environment variable called "PATH" and that's why it’s known as path. Whenever you type a command in shell in UNIX or Linux or in command prompt in windows machine, command will be looked on PATH and if shell is not able to find the command in PATH it says "not recognized" or wrong command. Now for compiling and running we use two java commands "javac" and "java" and these commands doesn't come by default with windows or Unix instead they comes 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




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 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 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 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



































Source:http://javarevisited.blogspot.com/2011/10/how-to-set-path-for-java-unix-linux-and.html

Tidak ada komentar:

Posting Komentar