Sei sulla pagina 1di 17

12/10/2017 Java ClassPath

Java ClassPath
What is PATH and CLASSPATH variables?

Home (/index.shtml) / Java (/java/index.shtml) / Java ClassPath

Questions: Ask (/answers/askquestions/askquestion.html) / Latest (/answers/questions/) / Tutorials:Latest (/latest.shtml) / Topics (/programming-tutorial/)

Java ClassPath
(index.shtml) (index.shtml) (index.shtml)

Question: What is the meaning of PATH and CLASSPATH ? How it is set in environment variable?.

ANS:- Significance of searching class path

Java is an OOP language. In java programming language for compiling the program we use the compiler that converts the source code into the byte code after that, that byte code is
interpreted by JVM that converts the bytecode into the machine independent code. In java how the Java compiler and the JVM use the class search path to locate classes when
they are referenced by other Java code. Searching class path is important for all Java developers.Many development tools have their own ways of manipulating the class path,
which vary from product to product.For doing this we will use only simple command-line tools to carry out the compile operations. No difference, between the way that the Java
compiler searches for classes, and the way that the JVM does it at run time. The compiler has the ability to compile classes from source code, where the JVM does not. We will use
the compiler, but similar issues apply at run time.

Searching of multiple class directories

javac(compiler)search the files in only one directory at a time.


But, class search path will contain numerous directories and JAR archives. The -classpath option to javac and java allows multiple entries to be specified, but the syntax is
different for Unix and Windows systems.
Advertisement

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 1/17
12/10/2017 Java ClassPath

For Unix like this

javac -classpath dir1:dir2:dir3 ...

For Windows like this

javac -classpath dir1;dir2;dir3 ...

The difference is that Windows uses the colon (:) character as part of a filename, so it can't be used as a filename separator. Naturally the directory separator character is different
as well: forward slash (/) for Unix and backslash (\) for Windows.
( I ). 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
(file:///C:/Java/jdk1.6.0_03/lib)

(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 (file:///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.

(ii)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. Chatbox


Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 2/17
12/10/2017 Java ClassPath

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 (file:///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.

(2) For setting CLASSPATH & PATH variable in environment variable by using the following steps:

(i) right click on my computer icon on desktop

(ii) click on properties

(iii) click on advanced

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 3/17
12/10/2017 Java ClassPath

(iv) click on Environment variables

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 4/17
12/10/2017 Java ClassPath

(v) system varables click on new

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 5/17
12/10/2017 Java ClassPath

(vi) in ( variable name) write ----- path

in ( variable value) paste the path till bin directory i.e.------- c:\java\jdk1.6.0_03\bin click on ok

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 6/17
12/10/2017 Java ClassPath

(vii) in system varables again click on new

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 7/17
12/10/2017 Java ClassPath

(viii) in ( variable name) write ----- classpath

in ( variable value) paste the path till lib directory i.e.------- c:\java\jdk1.6.0_03\lib click on ok

(ix) after finished click on ok

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 8/17
12/10/2017 Java ClassPath

(x) finished click on ok

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 9/17
12/10/2017 Java ClassPath

How to set System Classpath :

Java class path is set for including the various files during program execution & compilation.After setting class search path on the javac command line, we set `system' class path.
This class path will be used by both the Java compiler and the JVM . In both Unix and Windows systems, this is done by setting an environment variable. For example, in Linux with
the bash shell:

CLASSPATH=/jarclasses/jarclasses.jar;export CLASSPATH

for Windows:

set CLASSPATH=c:\jarclasses\jarclasses.jar

We use this procedure for setting short-term changes to the system CLASSPATH, but if you want these changes to be persistent you will need to arrange this yourself.For different -
2 system this path is different . On a Linux system, we put the commands in the file .bashrc in my home directory. On Windows 2000/NT there is a `Control Panel' page for this.

Setting the system CLASSPATH is a useful procedure if you have JARs full of classes that you use all the time.
Advertisements

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website. Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 10/17
12/10/2017 Java ClassPath

Custom Search Search

Previous (/java/index.shtml) Index (/java/java-classpath.shtml) Next (/java/index.shtml)

Spring Framework R Programming


Training Training
(http://www.roseindia.net/ittrai (http://www.roseindia.net/ittrai
training.shtml) programming-training-
course.shtml)
Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 11/17
12/10/2017 Java ClassPath

Home (/index.shtml) / Java (/java/index.shtml) / Java ClassPath

Questions: Ask (/answers/askquestions/askquestion.html) / Latest (/answers/questions/) / Tutorials:Latest (/latest.shtml) / Topics (/programming-tutorial/)

Java ClassPath Share on Google+


(https://plus.google.com/share?
url=http://www.roseindia.net/java/java-
classpath.shtml)

sted on: February 16, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles (https://plus.google.com/+roseindia/posts)
Advertisements

Related Tutorials

Why You Should Installing JDK 9 How to convert How to add two What is Applet in
Learn Java in on Windows 10 binary to decimal numbers in Java with
2018? (/java/java9/install in Java? Java? Example?
(/java/why-you- jdk-9-on- (/java/java- (/java/java- (/java/example/jav
should learn windows conversion/how conversion/how is applet in java
Like 4 Share
Tweet (http://twitter.com/share)

Discuss: Java ClassPath (/discussion/2282-Java-ClassPath.html) View All Comments (/tutorialhelp/allcomments/13648)

Post your Comment

Your Name (*) :

Your Email :

Subject (*):

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 12/17
12/10/2017 Java ClassPath

Your Comment (*):

SUBMIT

COMMENTS:17

prabhat ranjan
December 25, 2011
difficulty in setting of path of java.

Hi I have window7 operating system.and i have installed jdk-7u2-windows-x64.exe for java program. But i have a problem to set path of java. and also how to run java
program.

vinayaka D J
January 9, 2012
unable to execute jar command

i am trying to execute jar command but its showing command not found what to do
Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 13/17
12/10/2017 Java ClassPath

Sunil Sharma
February 1, 2012
How to set the class path.

Thanks to this services for java.

Pradeep Patel
February 18, 2012
Facing problem to set the classpath in java

Hello Sir I'm Pradeep Patel facing problem to set path and classpath for java programs

deepika
March 3, 2012
hi

i am getting noclassfound error while runtime

madhavi
June 29, 2012
java

I have set the path and classpath in environment variables.eventhough iam getting the error like javac:file not found:Demo.java usage java:<options> <source files> please
rectify my mistake if any

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 14/17
12/10/2017 Java ClassPath

Nejla
September 28, 2012
sYQaUsZUhBRjMz

My problem is the recoruse file is available in one context (say aa), I'm trying to load the same using ResourceBundle from another context (say bb). But always I get
MissingResourceException exception. But it works well when I put the properties file under bb context I think I can use the solution that you mentioned in this post for the time
being. But I'm searching for a solution so that I can use ResourceBundle(since the property file is for i18n purposes). Any way thanks dude it is indeed a good post.

abhishek kumar
November 2, 2012
how to set classpath temporary in java

two different folder having same package and same class name

Thiru
July 31, 2013
Java Class path

Its very simple and Working fine .. Thanks

Java Tutorials (http://www.roseindia.net/java/)


Java Code example (http://www.roseindia.net/javacodeexamples/index.shtml)
Java Programming (http://www.roseindia.net/java/java-programming.shtml)
Java Beginners Examples (http://www.roseindia.net/java/beginners/)
Applet Tutorials (http://www.roseindia.net/java/example/java/applet/index.shtml)
Awt Tutorials (http://www.roseindia.net/java/example/java/awt/index.shtml)
Java Certification (http://www.roseindia.net/javacertification/)
Interview Question (http://www.roseindia.net/interviewquestions/)
Java Servlets Tutorial (http://www.roseindia.net/servlets/servlets.shtml)
Jsp Tutorials (http://www.roseindia.net/jsp/jsp.shtml)
Java Swing Tutorials (http://www.roseindia.net/java/example/java/swing/) Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 15/17
12/10/2017 Java ClassPath

JDBC Tutorial (http://www.roseindia.net/jdbc/jdbc.shtml)


EJB Tutorials (http://www.roseindia.net/ejb/)
Java Server Faces (JSF) Tutorial (http://www.roseindia.net/jsf/)
WAP Tutorial (http://www.roseindia.net/wap/index.shtml)
Struts Tutorial (http://www.roseindia.net/struts/)
JAXB Tutorial (http://roseindia.net/jaxb/r/jaxb.shtml)
Spring FrameWork Tutorial (http://www.roseindia.net/spring/index.shtml)
SOA&Web Services Tutorials (http://www.roseindia.net/webservices/)
Bioinformatics Tutorials (http://www.roseindia.net/bioinformatics/bioinformatics_resources.shtml)
MySQL Tutorials (http://www.roseindia.net/mysql/)
JAVA DOM Tutorial (http://www.roseindia.net/xml/dom/)
XML Tutorial (http://www.roseindia.net/xml/dom/xml-tutorial.shtml)
EAI Articles (http://www.roseindia.net/eai/)
Many Programming Tutorials Links (http://www.roseindia.net/programming/tutorials/index.shtml)
Tutorials Books (http://roseindia.net/programming/books/index.shtml)
Java Script Tutorial (http://www.roseindia.net/javascript)
Ajax Tutorial (http://www.roseindia.net/ajax/)
Dojo Tutorials (http://www.roseindia.net/dojo/)
Programming Books (http://www.roseindia.net/programming/books/)
Trainings (http://www.roseindia.net/training/index.shtml)
Flex (http://www.roseindia.net/flex/)
Ant (http://www.roseindia.net/tutorials/ant/)
RDF (http://www.roseindia.net/tutorials/rdf/)

Search
Ads

Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 16/17
12/10/2017 Java ClassPath

TOP TUTORIALS

JAVA (HTTP://WWW.ROSEINDIA.NET/JAVA/INDEX.SHTML)

SPRING (HTTP://WWW.ROSEINDIA.NET/SPRING/INDEX.SHTML)

HIBERNATE (HTTP://WWW.ROSEINDIA.NET/HIBERNATE/INDEX.SHTML)

PHP (HTTP://WWW.ROSEINDIA.NET/PHP/INDEX.SHTML)

STRUTS (HTTP://WWW.ROSEINDIA.NET/STRUTS/INDEX.SHTML)
Chatbox
Live help is offline...

https://www.roseindia.net/java/java-classpath.shtml 17/17

Potrebbero piacerti anche