Classpath and Sourcepath

Class Path

The classpath in JSwat is used much the same way the Java Virtual Machine uses it. The classpath provides a list of locations where classes can be found on the system. The entries correspond to directories containing the classes concerned. For example, on my system the classpath is set to /home/nfiedler/java and all of my Java code resides inside that directory or subdirectories therein.

The way in which JSwat handles the classpath is quite simple. When the session is inactive, the classpath is simply that which was set via the classpath command or the "Set Classpath" dialog. If no setting is available, then JSwat takes the classpath from the "java.class.path" property, or if that property is not defined, the current working directory is used. When JSwat launches a new debuggee VM it sets the classpath of the debuggee VM to that which is set in JSwat. When JSwat is connecting to a remote debuggee VM then the classpath in JSwat becomes the classpath of the remote VM. Thus you will probably see a different classpath value depending on whether a remote debugging session is active or not.

When using either the load command or the "Start VM" dialog, you may provide an alternate value for the classpath. You do this with the "-cp" or "-classpath" Java VM options, just as you would when invoking 'java' from the command line. This will override (and overwrite) the classpath value defined in JSwat.

See the help for the classpath JSwat command to learn how to view and set the classpath used by JSwat. An easier way to modify the classpath is through the "Set Classpath" menu item in the "Options" menu.

Source Path

The sourcepath in JSwat looks just like a classpath setting. However, it is used only for finding source files. The sourcepath is used by JSwat as another means of finding the source file for a class. If a sourcepath is not set, JSwat will fall back on the classpath to find the source files.

The format of the sourcepath is exactly like the classpath. That is, if your source code is in /usr/source/stuff and your classes are in /usr/classes/stuff and the name of your class is stuff.Test, then the sourcepath should be set to /usr/source, just as the classpath is set to /usr/classes. You may also add Zip or Jar files to your sourcepath, and JSwat will find the source files inside of the named archives. Just as with the directories, the paths of the files in the archives must match the fully-qualified names of the classes (e.g. "java/lang/String.java" for "java.lang.String").

The sourcepath can be set one of two ways in JSwat. First, it can be passed when launching JSwat using the -D argument to the JVM, like so:

% java -Djava.source.path=/usr/java/src com.bluemarsh.jswat.Main

This value will override any previous sourcepath setting in JSwat. This is unlike the classpath, which defaults to the setting in JSwat rather than the java.class.path property setting.

The second way to set the sourcepath is with the sourcepath JSwat command. This works just like the classpath command and takes a set of directory paths and or Zip/Jar files as an argument. On Windows the directory paths are separated with ';' characters, while on Unix systems it is the ':' character. An easier way to modify the sourcepath is through the "Set Sourcepath" menu item in the "Options" menu.

How JSwat Finds Source Files

Apparently this is complicated because new users run into this problem with surprising frequency. It generally boils down to a misunderstanding of how the classpath and sourcepath are used. Keep in mind that JSwat uses the classpath pretty much the way you would expect. It is generally where Java source files (and class files) are found. The sourcepath is a set of alternative paths for finding source files, taking precedence over the classpath.

Let's look at a detailed example. Take for instance the class "java.lang.String". Let's assume the source file is called String.java and is located in /usr/java/src/java/lang. If the classpath is set to something like /usr/java/jre/lib/rt.jar then JSwat is not going to be able to find the source for java.lang.String since it has no idea where on the file system the file could be located.

This is where the sourcepath comes in. With the classpath set to /usr/java/jre/lib/rt.jar, JSwat only sees class files in that jar file. If we set the sourcepath to be /usr/java/src then JSwat will look for the file named /usr/java/src/java/lang/String.java. Lo and behold, there is the source code.

If this still does not make sense, read this page again until it does. I have explained this as simply as I can and provided multiple examples. If you cannot figure it out, you probably are confused with the classpath and should read a book on Java until you have grasped the concept.