[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Applet NetworkClassLoader

I'm trying to build a NetworkClassLoader that will load an instance of an object from a .class file located on a Web server on the net.  This will be inside of an applet, and the Web server where i load my object from will change..

Now, to get around the security of an Applet, I'm going to load this applet from my local file system.  I believe this will allow me to load classes from a server other than my Applet's codebase (at least that's what I understand from the documentation from Sun.)

I've tried to follow the example from the ClassLoader example in the API, but it doesn't work... just doesn't complete..

An example NetworkClassLoader would be invaluable to me..

thx, kurt colvin
0
colvin
Asked:
colvin
1 Solution
 
tattiCommented:
Not saying anything about ClassLoader, I'll just confirm that locally loaded applets are free from most of the security restrictions, including the restriction on servers they can connect to.
0
 
amarshalCommented:
the security restriction is dependent on the browser.  I beleive IE maintains the security restriction, unless it is loaded through the local classpath (Windows/java/classes.zip).  Applet classpath, even if froma local file system, are still secure.
0
 
colvinAuthor Commented:
Well....
I've pretty much figured out that what I want to do is not possible with the ClassLoader used in a browser.  I guess you can't do much to alter the classloader that the browser uses to load classes... so, i guess i'm going to switch my applet to an application.  

there is some new functionality in the new JDK 1.1 that may allow stuff like this with RMI and "signed" applets, but i'll let the real gurus try this stuff out first...

this service is a great idea... i noticed it was based in SLO, CA... are you guys Mustangs??? i was a few years back...

thx, kurt colvin
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
colvinAuthor Commented:
Still trying to do this... How about a functional NetworkClassLoader that will can actually load a class? This would be for an application.  I've seen several examples on the Web, but i can't get any of them to work.

thx, Kurt Colvin
0
 
rickl032797Commented:
This example is from the symantec help file, I have not tried it....

ClassLoader is an abstract Class that can be used to define a policy for loading Java classes into the runtime environment. By default, the runtime system loads classes that originate as files by reading them from the directory defined by the CLASSPATH environment variable (this is platform dependent). The default mechanism does not involve a Class loader.  

However, some classes may not originate from a file; they could be loaded from some other source, e.g., the network. Classes loaded from the network are an array of bytes. A ClassLoader can be used to tell the runtime system to convert an array of bytes into an instance of class Class. This conversion information is passed to the runtime using the defineClass() method.  
Classes that are created through the defineClass() mechanism can reference other classes by name. To resolve those names, the runtime system calls the ClassLoader that originally created the Class. The runtime system calls the abstract method loadClass() to load the referenced classes.  

        ClassLoader loader = new NetworkClassLoader(host, port);
        Object main = loader.loadClass("Main").newInstance();
        ....

The NetworkClassLoader subclass must define the method loadClass() to load a Class from the network. Once it has downloaded the bytes that make up the Class it should use the method defineClass() to create a Class instance. A sample implementation could be:

        class NetworkClassLoader {
            String host;
            int port;
            Hashtable cache = new Hashtable();
            private byte loadClassData(String name)[] {
                // load the class data from the connection
                ...
            }
            public synchronized Class loadClass(String name) {
                Class c = cache.get(name);
                if (c == null) {
                    byte data[] = loadClassData(name);

                    cache.put(name, defineClass(data, 0, data.length));
                }
                return c;
            }
        }

0
 
colvinAuthor Commented:
thanks for the reply..
i've tried this example too, but never could get it to work with my requriements..

i've since compromised by downloading the remote class to a file, then creating an instance from the file. not very pretty, but it works from what i'm doing. thx, kc
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now