Export (0) Print
Expand All

How to: Write a Class Loader

Visual Studio 2005

Occasionally, your applications might require a custom class loader that only instantiates classes based on your own criteria. This example shows how custom class loaders in existing Java-language applications can be modified to load classes from managed assemblies. The following example demonstrates how the common language runtime (CLR) can load classes based on whether the caller is a user or administrator.

NoteNote

Class loaders are only intended to be used by existing Java-language applications. For new code, it is recommended that classes be searched and loaded from managed assemblies using the .NET Framework's CLR semantics and APIs rather than using custom class loaders.

When writing class loaders, note the following considerations:

  • Avoid using the resolveClass and defineClass methods in the class java.lang.ClassLoader because they are unsupported.

  • In existing Java-language applications, rewrite the loadClass() method to call one of the versions of the .NET Framework Class.forName(), and then make appropriate entries in the application configuration (.config) file so that the CLR can locate and load managed assemblies for the application. If the managed assemblies reside in the application's working directory, then no configuration file is required and the Class.forName() search heuristics will locate the class.

To write a class loader

  1. On the File menu, point to New, and then click Project.

  2. In the New Project dialog box, select Console Application, and in the Name box, type ClassLoaderSample. Click OK.

  3. In Solution Explorer, double-click Program.jsl, and add the following code:

    class SecurityClassLoader extends ClassLoader 
    {
        protected Class loadClass(String className, boolean resolve)
                                 throws ClassNotFoundException 
        {
            if ("SecureClass" == className) 
            {
                String user = System.getProperty("user.name");
                if (null == user) 
                {
                    System.out.println("Class was not loaded. ");
                    System.out.println(
                    "user not found .name property is not set.");
                    return null;
                }
                else if (user.equals("Administrator"))
                {
                    System.out.println(
                    "Load access granted on the class 'SecureClass.'");
                    return Class.forName(className);
                }
            // If user is not an administrator.
            System.out.println(className + 
                        " was not loaded. Invalid class or username.");
            return null;
            }
            else if ("RegularClass" == className)
                return Class.forName(className);
            else
                throw new ClassNotFoundException();
        }
        
        public Class loadClass(String className) throws
               ClassNotFoundException 
        {
            return loadClass(className, true);    
        }
    } 
    
    public class ClassLoaderSample 
    {
        public static void main(String args[]) throws
               java.io.IOException 
        {
            SecurityClassLoader ccl = new SecurityClassLoader();
            
            try 
            {
                Class cl;
                Object obj = null;
                
                System.out.println(
                    "Attempting to load an instance of RegularClass.");
                cl = ccl.loadClass("RegularClass");
                if (null == cl)
                    System.out.println(
                    "Failed to load an instance of RegularClass.");
                else 
                    obj = cl.newInstance();
    
                if (null != obj)
                    System.out.println(
                  "Successfully loaded an instance of RegularClass\n.");
    
                obj = null;
                System.out.println(
                "Attempting to load an instance of SecureClass.");
                cl = ccl.loadClass("SecureClass");
                if (null == cl)
                    System.out.println(
                    "Failed to load an instance of SecureClass.");
                else
                    obj = cl.newInstance();
    
                if (null != obj)
                    System.out.println(
                    "Successfully loaded an instance of SecureClass.");
            }
            catch(Exception e) {
                if (e instanceof ClassNotFoundException)
                    System.out.println(
                    "Unable to find the specified class.");
                else if (e instanceof InstantiationException)
                    System.out.println(
               "Unable to create an instance of the specified class.");
                else if (e instanceof IllegalAccessException)
                    System.out.println(
                   "Do not have proper access to the class.");
            }
        }
    }
    
  4. Add a new class called SecureClass, by copying the following code:

    public class SecureClass 
    {
        SecureClass() 
        {
            System.out.println(
                      "In the constructor of class SecureClass");
        }
    }
    
  5. Add a second class called RegularClass, and replace the contents with the following code:

    public class RegularClass 
    {
        RegularClass() 
        {
            System.out.println(
                      "In the constructor of class RegularClass");
        }
    }
    
  6. Press F5 to build and run the sample.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft