![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e68. Loading a Class That Is Not on the ClasspathAURLClassLoader can be used to load classes in any directory.
// Create a File object on the root of the directory containing the class file File file = new File("c:\\myclasses\\"); try { // Convert File to a URL URL url = file.toURL(); // file:/c:/myclasses/ URL[] urls = new URL[]{url}; // Create a new class loader with the directory ClassLoader cl = new URLClassLoader(urls); // Load in the class; MyClass.class should be located in // the directory file:/c:/myclasses/com/mycompany Class cls = cl.loadClass("com.mycompany.MyClass"); } catch (MalformedURLException e) { } catch (ClassNotFoundException e) { }
e60. Getting the Name of a Class Object e61. Determining If a Class Object Represents a Class or Interface e62. Getting the Superclass of an Object e63. Getting the Superclass of a Class Object e64. Listing the Interfaces That a Class Implements e65. Listing the Interfaces That an Interface Extends e66. Getting the Package of a Class e67. Determining from Where a Class Was Loaded e69. Dynamically Reloading a Modified Class
© 2002 Addison-Wesley. |