Publishing System Settings Logout Login Register
How Classpath works in Java ?
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on April 5th, 2011
2872 views
Java Development

CLASSPATHis one of the most important concepts in java but I must say oftenlyoverlooked. This should be the first thing you should learn whilewriting java programs because without understand classpath you can'tunderstand how java locate your class files.

Solets start with basic and then we will some example and improvisationof classpath.CLASSPATH is an environment variable which is used by JAVAto locate user defined classes. You can specify value of CLASSPATH

In Windows by pressing "Windows + Pause “-->Advanced -->Environment variable "

In Linux you can simply export CLASSPATH="your classpath"

To check the value of classpath in windows type "echo %CLASSPATH" and to check value in Linux type "echo ${CLASSPATH}

Youcan override classpath defined by environment variable CLASSPATH byproviding option "-cp" or "-classpath" while running your program andthis is the best way to have different classpath for differentapplicaiton. General way to define classpath is in the startup script ofany program. e.g.
CLASSPATH=/home/tester/classes
java -cp $CLASSPATH Test

By default CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.

Incase you have multiple directories defined in CLASSPATH variable, javawill look for a class starting from first directory and only look seconddirectory in case it did not find the specified class in firstdirectory. This is extremely useful feature to understand and its veryuseful for patch release kind of stuff. Let’s see an example

I have my CLASSPATH=/home/tester/first:/home/tester/second
Now I have "Test" class of different version in both first and second directory so when I give a command
"java Test" What will happen ? Which Test class would be picked?

SinceJava search directory in the order they have listed in CLASSPATHvariable it will first go to the "first" directory and if it finds"Test" over there it will not go to "/home/tester/second" directory.

Nowif you delete Test class from /home/tester/first directory it will goto /home/tester/second directory and will pick "Test" class from there.

Ihave used this feature of java to test my patch releases, we used tohave a folder called "patch" listed as first element in CLASSPATH andany point of time we want to put any debug statement or want to test anybug we just modify class and put that inside patch folder instead ofrelasing whole new jar and this comes very handy if you are woking in alarge project where you don't have development environment setup inWindows and your project only runs on Unix server.

Itsalso worth noting that when you use the -jar command line  option torun your program as an executable JAR, then the CLASSPATH environmentvariable will be ignored, and also the -cp and -classpath switches willbe ignored.

In this case you can set your classpath in the META-INF/MANIFEST.MF file by using the Class-Path attribute.

Nowa common question if I have my CLASSPATH variable pointing to currentdirectory "." and I have class called "Test" inside package "testing"and with below directory structure C:\project\testing\Test.class in mycomputer.

What will happen if I run command "java Test" from directory "C:\project\testing\" will it run?


No it will not run, it will give you Exception in thread "main" java.lang.NoClassDefFoundError: Test
Since name of the class is not Test, instead its testing.Test even though your classpath is set to current directory.

Now what will happen if I give command “java testing.Test “from” C:\project\testing\, it will again not run and give error?

Exception in thread "main" java.lang.NoClassDefFoundError: testing/Test

Whybecause now it looking for class called Test which is in packagetesting , starting from current directory "." but don't find it sincethere is no directory called "testing after this path"C:\project\testing\".

To run it successfully you need to go back to directory "C:\project" and now run
C:\project>java testing.Test
It will run successfully.

Ihope you find this blog post useful , please let me know if you haveany doubt or any question related to "classpath in java" and I would behappy to answer :) keep learning.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
javabuddy

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top