Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Applet NetworkClassLoader

Posted on 1997-03-10
6
250 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
SHA2 certs for IIS AND Java? 2 113
Facing this issue for maven proxy setting 2 20
How  can  i  resolve  HTTP Status 404 -? 8 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

829 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