[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About

Classpath - getting acquainted

Classpath, files and class loading

2001 - Week 50 - Havard Rast Blok

Setting the classpath right is not always as easy as it should be. Every OS has its own pitfalls which you should be aware of. This week you will get familiar with your programming environment.

First you need to figure out how you set the classpath in your OS. As I said, this varies a lot so please refer to the Getting started section if you are not sure.

Keep in mind that in Windows 95, 98 and ME you need to set the classpath in the C:\AUTOEXEC.BAT file. (In Windows ME there might be other ways to handle this. Please correct me if I am wrong.) Afterwards you need to restart the computer.

While in Windows NT, 2000 and XP you find your way through Start -> Settings -> Control Panel -> System -> Advanced -> Environment variables. Here you only need to confirm the changes and restart any programs dependent upon the classpath, like DOS-prompt, Ultra Edit or the likes.

When the variable is set, you may view it in the DOS-prompt by typing echo %classpath% (Or in Linux: echo $CLASSPATH). In Windows NT, 2000 and XP set classpath has the same function.

When you have put your package directories in the classpath, you will now implement a small application which shows it on the screen, like the echo command above. In fact, your application should have exactly the same output as that command.

Hint: System.getProperty(String) and System.getProperties()


ShowCP.java

For this exercise you need two or more paths in you classpath variable. If you do not have several paths there, please add some for the sake of this application.

You will now change the application above slightly so that each path is displayed on a separate line. To achieve this, you need to separate each path using the semicolon (;) in Windows or colon (:) in Linux. Your application should work for both.

Hint: File.pathSeparator and StringTokenizer.


ShowCP.java

When you know the paths of your package directories, it would be interesting to see which classes are included in these packages.

Implement an application which searches through the classpath and lists all CLASS-files it finds. You may have to do some recursive searching here, if your classes are located in sub directories.

Hint: remember the dir/ls exercises

My solution requires a file filter called com.rememberjava.io.SimpleFileFilter. Make the package directory and download the file SimpleFileFilter.java by clicking the package icon to the left.


ShowClasses.java


com.rememberjava.io.SimpleFileFilter.java

Finally, this week, if you still have some time to Remember Java, you will extend the application above to check if the classes found can be loaded. That this, for each CLASS-file you locate you need to figure out the correct class name (including package name) for this file and try to load it. Your application should list one class file on each line and whether the loading succeeded or failed.

Hint: Class forName(String className)

My solution requires a file filter called com.rememberjava.io.SimpleFileFilter. Make the package directory and download the file SimpleFileFilter.java by clicking the package icon to the left.


CheckClasses.java


com.rememberjava.io.SimpleFileFilter.java



site: Håvard Rast Blok
mail:
updated: 16 July 2010