Thursday, April 21, 2011

Path and classpath in java

For running a simple program we need to set the java path on command prompt(for temporary )& in Environment variable using PATH variable & CLASSPATH variable :

PATH variable 
In JDK the PATH variable contains directories where binary files (e.g. EXE files in Windows) will be looked for.We set the PATH variables like this i.e path C:\Java\jdk1.6.0_03\bin 

(i) On command prompt
C:\>set path=%path;C:\Java\jdk1.6.0_03\bin%
When you open a command prompt and type "javac", you're supposed to have the "bin" directory of your sdk into the PATH, otherwise you'll get an infamous "Command not found" error message.

CLASSPATH 
In JDK the CLASSPATH contains directories (or JAR files), from where your java compiler/runtime will look for .class files (and some others). For example, "java Hello.class" will not work unless you set the directory (or JAR file) Hello.class is in, into your CLASSPATH.
i.e.classpath  C:\Java\jdk1.6.0_03\lib
For setting CLASSPATH using command prompt  Java class path can be set using either the -classpath option when calling an SDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

On command prompt 
C:\>set classpath=%classpath;C:\Java\jdk1.6.0_03\lib%

JARs on the classpath
Java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. A JAR file can maintain its own directory structure, and Java follows exactly the same rules as for searching in ordinary directories. Specifically, `directory name = package name'. Because a JAR is itself a directory, to include a JAR file in the class search path, the path must reference the JAR itself, not the directory that contains the JAR. This is a very common error. Suppose I have a JAR jarclasses.jar in directory /jarclasses. The Java compiler look for classes in this jar, we need to specify: javac -classpath /jarclasses/jarclasses.jar ...
and not merely the directory jarclasses.  

For the CLASSPATH  use  CLASSPATH Environment Variable 
In java programming language we use the following packages such as  java.io.*;  java. util.*;These  packages are  just a set of classes, you will want to store them in one place, separate from the directory where you are developing your program. Sometimes these libraries have a set of files stored in a single jar file ( with a .jar extension). From where  the Java compiler and run programs must be able to find any of these. This is done for command line use of Java and for some editors and IDEs by setting an environment variable called CLASSPATH. For  Windows, the CLASSPATH environment variable should look like this :
c:\MyJavaLib;c:\MyJavaLib\myutils.jar;c:\MyJavaLib\blackboxclasses.jar;.
At the end " . "  includes the current directory in the search for classes.
  • Suppose we have a directory MyJavaLib on the C-drive that contains some utility classes or the directories that correspond to some packages. This  is the part before the first semi-colon.
               
  • Second part indicates that we have some classes stored in a file myutils.jar
             
  • Third part indicates that we have a jar file blackboxclasses.jar. 
Finally, after the last semi-colon we have the period( . ), indicating that the current directory is on the CLASSPATH. If some things are stored in directories that are subdirectories, the complete path, starting with "c:\" should be in the CLASSPATH.

Setting classpath in windows 7,Vista and xp graphically

No comments:

Post a Comment

Chitika