Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Using URLClassLoader [JDK 1.2]

 
Training Index


Help | Solution | API Docs | Expected Output
Course Notes | Magercises | Module Intro

Prerequisites

Skeleton Code

This magercise duplicates what was done in the ClassLoader magercise for JDK 1.1, but uses JDK 1.2's URLClassLoader, instead of manually creating a custom class loader. As before, instead of working with the CLASSPATH environment variable, you tell the class loader a specific directory to load class files from. The class loader will load any support files from the CLASSPATH, as classes like Object must come from trusted locations.

See the Class Loading course section for additional information.

Perform the following tasks:

  1. In the URLTester.java skeleton, create an array holding the URL of the file (directory name) specified from the command line, as args[0].

  2. Create an instance of URLClassLoader, using this array in its constructor.

  3. Next, have the test program load a class whose name will be provided from the command line, also, as args[1].

  4. After loading the class, create an instance of it. You can use reflection and the Constructor object, or just the newInstance method if the class has a no argument constructor.

  5. The Tester.java file contains a test class to use for the class loader. The only thing it does is print messages when the class file is loaded and instances are created. Save and compile the file.

  6. If you have a "." in your CLASSPATH, move the Tester.class file to another directory.

  7. Run the URLTester program. The first argument to the program is the directory you just moved the Tester.class file to. The second argument is the class you wish to load, Tester.

  8. To demonstrate the flexibility of the URLClassLoader, place the Tester.class file in a JAR file. Then, tell the URLTester program to load the file from the JAR file, instead of a directory.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and a demonstration of the expected behavior.

Copyright © 1998 MageLang Institute. All Rights Reserved.