Solved

Applet NetworkClassLoader

Posted on 1997-03-10
6
245 Views
Last Modified: 2013-12-29
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
Comment
Question by:colvin
6 Comments
 
LVL 1

Expert Comment

by:tatti
ID: 1219034
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
 

Expert Comment

by:amarshal
ID: 1219035
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
 

Author Comment

by:colvin
ID: 1219036
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:colvin
ID: 1219037
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
 

Accepted Solution

by:
rickl032797 earned 200 total points
ID: 1219038
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
 

Author Comment

by:colvin
ID: 1219039
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
splitOdd10 challenge 5 104
firstswap challenge 20 77
Chrome and Firefox Java 5 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now