What is a classpath and why do I care?

When starting out in Java one of the things that confused me the most, and coincidentally is one of the primary strengths of Java, is the concept of a classpath and libraries (.jar files). I want to take some time to go over both of them and hopefully simplify it.

When starting out in Java you will end up doing a lot of work from the command line, in Windows it is your C:\ prompt.  I know, you thought you’d never use it again once Microsoft put a nice GUI on MS-DOS. From the command line you will run commands like ‘javac’ and ‘java’ over and over again to compile and run your programs.  Unfortunately Windows does not natively recognize these commands.  Therefore you have to tell Windows how to recognize them.

That’s where our Path comes in, the close cousin of the classpath.  In Windows you can view your Path by going to Start/My Computer/(right-click) on My Computer/properties/Advanced tab/Environment Variables.  You’ll see an entry in the System variables called ‘PATH’  When installing Java, you need to put the path to your jdk_xx_xx_xx.xxxx/bin directory into your PATH so that Windows knows where to look for your java commands (java, javac, etc…).  My Windows PATH looks like this where JAVA_HOME has been defined as C:\Program Files\Java\jdk1.6.0_11.

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%JAVA_HOME%\bin;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin;%MAVEN_HOME%\bin;%ANT_HOME%\bin

After installation you will mostly modify your application classpath you work in Java. Think of your CLASSPATH as a toolbox.  If you are going to build a shelf you need a saw, measuring tape, nails, and a hammer.  These are the tools required for the job.  When you sit down to build the shelf you can’t just sit down anywhere and start building.  You need to make sure your toolbox is with you and that it includes your saw, measuring tape, nails, and hammer.

This is the same thing as your CLASSPATH. Java includes some basic tools, such as the java.util class, java.io class, and a few others.  Think of these classes as your basic tools: hammer, screwdriver, and saw.  It is difficult to do any projects without a saw, hammer, or screwdriver and the same goes for Java. Therefore Java includes some of necessary tools in your toolbox and it makes sure you always have your toolbox.  You still need import statements but you don’t need to modify your CLASSPATH.

However, if you are going to finish your new shelf you will need some sandpaper, stain, varnish, and some rags.  Therefore you need to go to Home Depot and buy some materials and put them into your toolbox. Think of the Internet as Home Depot.  And think of sandpaper, stain, varnish, and rags as .jar files.  .jar is not a jelly jar, not a honey jar, and definitely not Jar Jar Binks.  .jar stands for Java Archive and is a collection of classes that perform a specific function.

Let’s say you want to use jUnit to perform some testing when your java program compiles, like Part A of the Johns Hopkins example. You need to work with the jUnit API (Application Programming Interface).  To do this with Java you add import statements at the beginning of your class so that Java knows to look for some additional tools. Once you import the classes from the API, then you can use the objects and methods described by them.

However, when you compile your program, the Java compiler has no idea where to find the classes described by the import statements.  This leads to an error something like: package junit.framework does not exist.  (Which leads you to say something like: “&#!&&, I just downloaded it and put it in the directory, what do you mean it doesn’t exist!”).  You have to tell the compiler where to look by specifying your CLASSPATH when compiling.  There are two ways to do this:

  1. You do this by adding the -classpath parameter along with the path to the necessary .jar file to your javac command (something like:  javac -classpath c:/jhu/repository/junit/junit/3.8.1/junit-3.8.1.jar AppTest.java)
  2. You can add the path to the .jar file to your Windows CLASSPATH variable (found in the same place as the Path variable).  This is not a good idea as you start doing Web programming.  You will most likely not have access to your application server’s classpath so it is a good idea to get used to option 1

You will run into this over and over again in Java.  The reason Java has so much power and flexibility is directly related to the ability to import various .jar files (APIs) to add power and functionality to your application.  Programs like Maven and Ant help out considerably with compiling and running programs. They automate option 1 above so that you don’t have to specify a lengthy classpath every time you run your application. Even more helpful still are IDE’s like Eclipse and Netbeans.  They will help facilitate the building of your classpath so that it is almost invisible to you. Even if you are starting with an IDE it is a good idea to understand what is happening “under the hood” of these programs.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s