Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Help is available for each task, or you can go straight to the solution source code.



Task 1

Working from the FileClassLoader.java skeleton, have FileClassLoader subclass ClassLoader.


extends ClassLoader

Task 2

Find out if the requested class to loadClass is already loaded. Remember to check for system classes, too.


Class c = findLoadedClass (name);
if (c == null) {
  try {
    c = findSystemClass (name);
  } catch (Exception e) {
    // Ignore these
  }
}

Task 3

If the class isn't loaded, you'll need to convert the class name to a file name and load the data from the class. The filename conversion is already done for you and the loading of the data is already done. The only thing you need to do is connect everything. Just call the private loadClassData method with the filename for the class. The method returns the data from the file in a byte array, so you'll need to save this.


byte data[] = loadClassData(filename);

Task 4

Once you have the class data in a byte array, convert the byte array into a Class. In the event the byte data is invalid, throw an exception.


Be sure to use the four argument variety of defineClass. The three argument version, without the class name, is deprecated. If you happen to not know the name of the class being created, you can use null as the first argument.
c = defineClass (name, data, 0, data.length);
if (c == null)
  throw new ClassNotFoundException (name);

Task 5

Resolve the class definition if appropriate.


if (resolve)
  resolveClass (c);

Task 6

Return the class object just created.


return c;

Task 7

In the CLTester.java skeleton, create an instance of the FileClassLoader. Have the directory name that you use to load files from, come from the command line, as args[0].


FileClassLoader loader = new FileClassLoader(args[0]);

Task 8

Next, have the test program load a class whose name will be provided from the command line, also, as args[1]. The public version of loadClass() doesn't have a resolve argument, and defaults to true.


Class c = loader.loadClass (args[1]);

Task 9

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.


Object tester = c.newInstance();

Task 10

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.


Shift-click on the Tester.java link to save. Then compile with the following command:
javac Tester.java

Task 11

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


Something like the following will do. Depending upon your platform, the specific command may vary.
mkdir test
move Tester.class test

Task 12

Run the CLTester 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 want to load Tester.


java CLTester test Tester
Copyright © 1998 MageLang Institute. All Rights Reserved.

[ This page was updated: 15-Nov-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.