Recent Posts

Responsive Ads Here

Saturday, 5 March 2016

What is Path and Class Path in java ?

To run and compile java programs, we need an Environment.JDK provide java environment through PATH and CLASS PATH either command-line (or) Environment variables. Command line for Temporary setting,environment variables for Permanent setting  paths.

PATH :- PATH is where all Java libraries ,supported jars like javac,javah ,java,javap ,etc .All tools are lies in JDK bin folder ,so we need to set path for run our program successfully.

suppose if you want to compile a java program ,we do like this.

C:\Program Files (x86)\Java\jdk1.8.0\bin>javac  Test.java

Javac.exe file checked  on (C:\Program Files (x86)\Java\jdk1.8.0\bin ) when we press enter for above program Test.java .So we happily compile our program ,because javac.exe file lies in bin folder of JDK.


What  will happen  ? if we are not set path and compile a program?
My intension is, compile above program.so for that i just set path like this
C:\ >set path=”C:\>Program Files (x86)>”;
C:\>javac Test.java
In Program Files (x86) there is no javac.exe file to compile our program,so we will get an error on command program “ javac is not recognized as an internal or external command




Class Path :- The location where required .class files are available  JVM or java compiler that specifies the location of user defined classes and packages.
When we executing java programs ,the JVM finds and loads classes.The class path tells jvm ,where to look in the file system for  defining there classes.
To run program ,we should pass our .class to JVM
The JVM searches and loads classes in this order through rt.jar
       1.Boostrap classes
       2.Extension classes
       3.User defined packages and libraries

         

Setting class path as temporary
C:\Program Files (x86)\Java\jdk1.8.0\bin> set ClASSPATH=”C:\Program Files (x86)\Java\jre8\lib\rt.jar>”;

C:\Program Files (x86)\Java\jdk1.8.0\bin>java Test 







No comments:

Post a Comment