Dynamic Loading.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Dynamic Loading.

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Dynamic Loading.

    Hello all,

    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...

    Code:
       Class c = Class.forName( "com.mindprod.mypackage.Myclass" );
       Thing t = (Thing)c.newInstance( );
       t.doSomething();
    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...

    * 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,

    Thanks heaps,

    Andrew.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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?

    Code:
    forName
    
    public static Class<?> forName(String name,
                                   boolean initialize,
                                   ClassLoader loader)
                            throws ClassNotFoundException
    
        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:
    
              Class.forName("Foo")
             
    
        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.
    
    
        Parameters:
            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 
        Returns:
            class object representing the desired class 
        Throws:
            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
        Since:
            1.2
        See Also:
            forName(String), ClassLoader
    Take a look at the API documentation for ClassLoader ...
    Last edited by nspils; 08-29-2007 at 09:25 AM.

Similar Threads

  1. Dynamic Menu loading
    By T O'Pry in forum .NET
    Replies: 0
    Last Post: 09-10-2002, 03:36 PM
  2. Dynamic Class Loading & Design Patterns
    By Zeb Fropiaz in forum Java
    Replies: 1
    Last Post: 03-29-2002, 08:02 PM
  3. Dynamic Class Loading & Design Patterns
    By Zeb Fropiaz in forum Java
    Replies: 0
    Last Post: 03-17-2002, 12:57 PM
  4. dynamic .JS Loading
    By jay in forum Web
    Replies: 0
    Last Post: 07-27-2001, 02:32 PM
  5. Replies: 1
    Last Post: 08-01-2000, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center