applet codebase, archive and forName

T

tekinico

Hi there !

I'm having issues to make my applet running correctly !
Situation: I have one html page with an applet tag to run an applet
called "Monitor" (Monitor.class). This class is in the same directory
on the server, so i don't have any problem with, it loads.

My problem is that this class Monitor dynamically loads another class
and displays it in the Monitor panel :
Class simulatorClass = Class.forName(simulatorClassName);
simulator = (Applet)simulatorClass.newInstance();

The problem is that this "Simulator" class is not in the same folder on
the server.
So, first i tried to give the loader a url and try like that :
URLClassLoader loader = new URLClassLoader(new URL[] { new
URL("http://lblablabla.com/appletFolder/") });
// Load class from class loader.
Class c = loader.loadClass (simulatorClassName);
// Create an instance of the class just loaded
simulator = (Applet)c.newInstance();

But for some security reasons, the jvm on the browser machine doesn't
allow this.

Question: How can i do to load my applet in a different folder ?? I've
been trying every possible option with the codebase and archive
attribute on the html page as well, but wihtout success !!

Thanks in advance !

Nico
 
A

Andrew Thompson

tekinico wrote:
....
I'm having issues to make my applet running correctly !

URL? (link to the code)
But for some security reasons,

Care to be more specific? Copy/pasted stacktraces*
are generally better received than vague descriptions.

* Or at least, the first couple of lines.
Question: How can i do to load my applet in a different folder ??

Show us the URL and stacktrace, and we will probably be
able to figure what is wrong.

Andrew T.
 
T

tekinico

ok, sorry for the lack of information
http://tekinico.free.fr/sharing/

You can find the Monitor.class & java file. It's the loader
In the "temp" directory" is the content.class & java file that i want
to load in my Monitor.class file with this code :

URLClassLoader loader = new URLClassLoader(new URL[] { new
URL("http://tekinico.free.fr/simulator/") });
// Load class from class loader. argv[0] is the name of the class to be
loaded
Class c = loader.loadClass ("Content");
// Create an instance of the class just loaded
simulator = (Applet)c.newInstance();

And it gives me this error :

java.security.AccessControlException: access denied
(java.lang.RuntimePermission createClassLoader)
java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JApplet.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Monitor.init(Monitor.java:65)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I think the error is the first one, the nullPointerException being
generated because the loading of the Content class was unsuccessful.
The accessdenied exception seems to be generated because i try to load
a class from a remote site and the browser's jvm don't trust it and
can't check it before being ran, so it denies the loading.


Thank you for your help.
 
C

Chris Uppal

tekinico said:
URLClassLoader loader = new URLClassLoader(new URL[] { new
URL("http://tekinico.free.fr/simulator/") });

I think the problem is that unsigned applets are not allowed to load classes
from any codebase other than the one from which the applet itself was loaded.

You could try messing with a signed applet, but I don't understand why you need
to use a separate classloader in the first place. Why not just put all the
classes into the applet's code base, and not bother with a custom classloader
at all ?

-- chris
 
T

tekinico

I see your point, but i need to do this because this applet will later
be used to "monitor" the use of several other applets which are
simulators. There is about 50 to 100 simulators, each one having its
own folder "img" for example. I need to separate all those files to
make sure the final program is clear and clean. That's why i want to
have different codebases.

Do you think that if i get my applet signed, i'll be able to avoid the
problem ?
Isn't there a way to tell my program that the simulator is in a close
folder instead of puting the whole url
"http://blablabla.com/myappletfolder/"; like just "./myappletfolder/".
It is weird that i can only load an applet from the same filepath but
not from another folder on the SAME server...

Thanks for your help

Chris said:
tekinico said:
URLClassLoader loader = new URLClassLoader(new URL[] { new
URL("http://tekinico.free.fr/simulator/") });

I think the problem is that unsigned applets are not allowed to load classes
from any codebase other than the one from which the applet itself was loaded.

You could try messing with a signed applet, but I don't understand why you need
to use a separate classloader in the first place. Why not just put all the
classes into the applet's code base, and not bother with a custom classloader
at all ?

-- chris
 
A

Andrew Thompson

Chris said:
tekinico said:
URLClassLoader loader = new URLClassLoader(new URL[] { new
URL("http://tekinico.free.fr/simulator/") });

I think the problem is that unsigned applets are not allowed to load classes
from any codebase other than the one from which the applet itself was loaded.

They are also restricted from changing the class loader,
or adding a class loader, for much the same reasons.
You could try messing with a signed applet, ....

And to the OP. I am not yet convinced that this applet
needs a custom clas loader.

Anything at the same domain is accessible via an
URL, and if you are not adding the resource to the
archives attribute of the applet, you can simply
get it that way. The advantage of adding the resources
(other applets, in thise case) to the archives
attribute/class path is that you can use getResource()
to discover the URL.

Andrew T.
 
T

Thomas Hawtin

tekinico said:
I see your point, but i need to do this because this applet will later
be used to "monitor" the use of several other applets which are
simulators. There is about 50 to 100 simulators, each one having its
own folder "img" for example. I need to separate all those files to
make sure the final program is clear and clean. That's why i want to
have different codebases.

You should be able to copy the relevant files in your build process. You
are aware that the same class loaded from a different codebase will be a
different Class?
Do you think that if i get my applet signed, i'll be able to avoid the
problem ?

Unless you understand all the security implications of signed code
enough to realise that it's a bad idea, it's a bad idea.

In any case, URLClassLoader.newInstance is what you are looking for.

Tom Hawtin
 
T

tekinico

First of all, thank you all for your help/feedback !
You should be able to copy the relevant files in your build process. You
are aware that the same class loaded from a different codebase will be a
different Class?

I don't see how i would be able to do that, the classes i need to load
in this applet are quite random, and i want, in the future to be able
to add more classes to the server that could be accessed the same way

client -----loads-----> monitor -----loads-----> random class
Unless you understand all the security implications of signed code
enough to realise that it's a bad idea, it's a bad idea.

I don't know much of security implications about that, i'm quite new in
applets, but it seems not a clean way to do what i want to do, i
confess.
The advantage of adding the resources
(other applets, in thise case) to the archives
attribute/class path is that you can use getResource()
to discover the URL.

I'll try to use it as well as resources files for the applets i want to
load. But if i archive all my applets one by one, i will have to recode
their access to their own resources. (I precise that these applets are
not programmed by me, they're given to me)


Thank u all
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top