very useful forum you guys have here. maybe you might be able to help me with an issue i am having with a program i am writing? I am trying to dynamically load a class that the user specifies. I am coding in standard java 1.5 and swing. what i have found so far is as follows...
The forName() method takes a String that is the *NAME* of the class, my program uses a JFileChooser and the method getSelectedFile that returns a (.class) File that needs to be dynamicaly loaded. I need a way to do this, some thought would be...
Class c = Class.forName( "com.mindprod.mypackage.Myclass" );
Thing t = (Thing)c.newInstance( );
* Get the path of the returned file and somehow turn it into a class name.
* Some other way of loading a class based upon the file path not name.
If anyone has any ideas i will be very grateful,
Did you look at the other "forName" method, which includes as an argument an instance of a "ClassLoader" you can use to construct an instance of the class you name with the other argument?
Take a look at the API documentation for ClassLoader ...
public static Class<?> forName(String name,
Returns the Class object associated with the class or interface with the
given string name, using the given class loader. Given the fully qualified name
for a class or interface (in the same format returned by getName) this method
attempts to locate, load, and link the class or interface. The specified class
loader is used to load the class or interface. If the parameter loader is null,
the class is loaded through the bootstrap class loader. The class is initialized
only if the initialize parameter is true and if it has not been initialized earlier.
If name denotes a primitive type or void, an attempt will be made to locate
a user-defined class in the unnamed package whose name is name. Therefore,
this method cannot be used to obtain any of the Class objects representing
primitive types or void.
If name denotes an array class, the component type of the array class is
loaded but not initialized.
For example, in an instance method the expression:
is equivalent to:
Class.forName("Foo", true, this.getClass().getClassLoader())
Note that this method throws errors related to loading, linking or initializing
as specified in Sections 12.2, 12.3 and 12.4 of The Java Language
Specification. Note that this method does not check whether the requested
class is accessible to its caller.
If the loader is null, and a security manager is present, and the caller's
class loader is not null, then this method calls the security manager's
checkPermission method with a RuntimePermission("getClassLoader")
permission to ensure it's ok to access the bootstrap class loader.
name - fully qualified name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
class object representing the desired class
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization provoked by this method fails
ClassNotFoundException - if the class cannot be located by the specified class loader
Last edited by nspils; 08-29-2007 at 08:25 AM.
Last Post: 09-10-2002, 02:36 PM
By Zeb Fropiaz in forum Java
Last Post: 03-29-2002, 07:02 PM
By Zeb Fropiaz in forum Java
Last Post: 03-17-2002, 11:57 AM
Last Post: 07-27-2001, 01:32 PM
Last Post: 08-01-2000, 01:51 PM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL