What is PATH and CLASS-PATH all about in JaVa ?

What is PATH and CLASS-PATH 



Many problems in the installation and running of Java applications are caused by incorrect setting of environment variables (global system variables available to all the processes running under the system), in particular, PATH, CLASS-PATH.

PATH Variable:

PATH is basically an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

When you launch a program from the command line, the operating system uses the PATH environment variable to search for the program in your local file system. PATH maintains a list of directories for searching executable programs.

If the program cannot be found in these directories, you will get an error. For example, if Java Compiler "javac.exe" is not found in the current directory and all the directories in the PATH, you will receive this error when compiling java source code:


ERROR:javac is not recognized as an internal or external command.



 For Java applications, PATH must include the following directories:
  • JDK's "bin" directory (e.g., "c:\Program Files\java\jdk1.7.0_{xx}\bin"), which contains JDK programs such as Java Compiler "javac.exe" and Java Runtime "java.exe".
  • "c:\windows\system32" and "c:\windows" which contain console programs and commands.

How To set Path in java?

There are 2 ways to set java path:
  1. Temporary.
  2. Permanent.
1.Temporary path of JDK in windows:

  • Open command prompt
  • copy the path of jdk/bin directory
  • write in command prompt: set path=copied_path

Let's see it in the figure given below:


2.Permanent path of JDK in Windows:
  • Right click on 'My Computers' and open 'Properties'.
  • In Windows Vista or Windows 7, go to "Advanced System Settings". Else go to next step.
  • Go to 'Advanced Tab' and click on Environment Variables button.
  • click on new tab of user variables
  • write path in variable name and paste path of bin folder in variable value.
  • Click on OK botton.

Setting JAVA path in Linux OS:

Setting the path in Linux OS is same as setting the path in the Windows OS. But here we use export tool rather than set. Let's see how to set path in Linux OS:

export PATH=$PATH:/home/jdk1.6.01/bin/


CLASS-PATH in java:

The CLASS-PATH is an Environment variable is one way to tell applications, including the JDK tools, where to look for user classes. Class-path in Java is the path to directory or list of the directory which is used by Class-Loaders to find and load classes in Java program.

It would be impractical to have the JVM look through every folder on your machine, so you have to provide the JVM a list of places to look.  Therefore you need to set your class-path such that, JVM should be able to locate all your resources like classes, files, jars etc.


Setting CLASS-PATH:

In order to set Classpath for Java in Windows you need to specify the value of environment variable CLASSPATH, the name of this variable is not case sensitive and it doesn’t matter if the name of your environment variable is Classpath, CLASSPATH or classpath in Java.

Steps:

    How to se Java Classpath in windows and Unix Linux
  1. Go to Environment variable window in Windows by pressing  or you can go from rig"Windows + Pause “--> Advanced --> Environment variable "ht click on my computer than choosing properties and then Advanced and then Environment variable this will open Environment variable window in windows.
  2. Now specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and also include CURRENT DIRECTORY by including (dot or period sign).
Now to check the value of Java classpath in windows type "echo %CLASSPATH" in your DOS command prompt and it will show you the value of directory which is included in CLASSPATH.

  • You can also set classpath in windows by using DOS command like:
set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;

This way you can set the class-path in Windows XP, windows 2000 or Windows 7 and 8, as they all come with command prompt.



Setting CLASS-PATH in LINUX:

To set Class-path for Java In Linux, you can simply export CLASSPATH="your classpath" from either your .bash_profile or .bashrc script which will run whenever your login into your Linux or Unix Machine. Now to check the value of Java CLASSPATH in Linux type "echo ${CLASSPATH}" this will print the value of Classpath in command prompt. By using the export command, you can set the classpath for Java in Unix, Linux, Solaris, or any other UNIX operating system. 


What are main differences between CLASSPATH and PATH?

  1. Path is an environment variable which is used by the operating system to find the executables. Classpath is an environment variable which is used by the Java compiler to find the path, of classes.i.e in J2EE we give the path of jar files.
  2. PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables. Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes.
  3. Path refers to the system while classpath refers to the Developing Environment.

Want to learn more about java?


Comments

  1. great job! Much needed.... Thank you...
    Can you write a blog on linked list?

    ReplyDelete
  2. Very nice content
    Will you advice me which IDE should I use for java programming?

    ReplyDelete

Post a Comment

Popular posts from this blog

PPC - SomeOne SpenDing - SomeOne EarNing

Web scrapping tools,Sooo Muuch Data - Analysis Needed !

Must kNow Web Dev toolS - 2OI7