Solved

Applet NetworkClassLoader

Posted on 1997-03-10
6
235 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

21 Experts available now in Live!

Get 1:1 Help Now