What Is a Classpath and How to Set It

What is a classpath and how do I set it?

When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying:

import org.javaguy.coolframework.*;

So later in your program when you say:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class.

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

  • JAR files, and
  • Paths to the top of package hierarchies.

How do you set your classpath?

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

The second way is to use the -cp parameter when starting Java, like this:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter.

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (':') is the classpath separator. In windowsm the separator is the semicolon (';')

So what's the best way to do it?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.

The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).

Can't understand classpath for the life of me

Let's assume that your command prompt's current directory is C:\newApp and a class called Test lives in C:\newApp\Test.java.

The classpath is simply the location, or set of locations where class files can be found, that your program might need to use. The classpath includes folders, JARs, and (in certain more complex setups) other sources of classfiles.

When Java needs to load a class, it looks on the classpath for it. The default classpath is the set of Jars that contain Java's built-in classes, combined with the current directory of your shell prompt (C:\newApp in this case).

When you try to load a class, say com.example.MyClass, Java will look in your classpath, in the location C:\newApp\com\example\MyClass.class. You can tell it to get the class from a JAR (which is nothing more than a ZIP archive with a .jar file extension) by putting said JAR on the classpath. It'll look inside the jar and "unzip" classes from there as needed.

When using an IDE, you do not generally need to worry about the classpath. The IDE will automatically put your code in the classpath automatically when running. If you want to use code from an external library, you simply need to tell your IDE to add it to the project (usually by putting it into your project folder and right-clicking it in the file list). It will then know that its autocomplete functionality should include available classes from that Jar, and that the Jar is to be placed on the classpath when running your project.

What is the purpose of setting a CLASSPATH in Java?

WHY DO WE SET THE CLASS-PATH?

well this is an riveting question.

it is self explanatory as

CLASSPATH is an environment variable and contain

paths to the jar files and path to various packages.

KISS ANSWER TO YOUR QUERY:

Perhaps ,the reason behind successful execution of
your code is that the JVM checks the current directory
first for the perception of jar files and then follows
the classpath .So your current directory has those files.

OH!You may wonder then why would we set the classpath
.Setting the classpath overrides that by default path.

Happy Learning :)

What are Java Classpath and Path?

A path is just a folder location. The path is where your OS will look for programs by default. If java, javac, javap, etc, etc, are in your path then you can just type their names without the entire folder location.

Your classpath is similar. It is a set of folders that contain .class files describing classes(hence the name) and .jar files, which are basically files that contain .class files. All code that you're running is either out of the classpath, generated, or out of the java libaries(also part of the classpath, techncically).

With each run of a java program you can specify a classpath by parameters passed to the java executable. It also grabs classes out of "extension folders,", special folders Java keeps around to act as a system classpath, and finally, the "bootstrap classes", which are a set of important classes almost any Java program needs to run.

What does a properly configured CLASSPATH variable for JDBC use look like?

Classpath should include all classes that JVM needs to run your application: all dependencies and your application classes.

You can pass classpath for each java invocation like

java -jar <jar file>

or

java -cp <your full classpath goes here> <your main class>

But you can also use build tools to either build fat jar with dependencies and configured classpath or build zip with main jar, dependencies is a separate folder and preconfigured classpath set in your jar.

How should I set CLASSPATH?

Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/

Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards"



Related Topics



Leave a reply



Submit