Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

applet codebase, archive and forName

Thread view: 
tekinico - 27 Oct 2006 04:49 GMT
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
Andrew Thompson - 27 Oct 2006 05:40 GMT
...
> 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.
tekinico - 27 Oct 2006 06:43 GMT
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.

> ...
> > I'm having issues to make my applet running correctly !
[quoted text clipped - 14 lines]
>
> Andrew T.
Chris Uppal - 27 Oct 2006 10:52 GMT
> 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
tekinico - 27 Oct 2006 12:10 GMT
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

> > URLClassLoader loader = new URLClassLoader(new URL[] { new
> > URL("http://tekinico.free.fr/simulator/") });
[quoted text clipped - 8 lines]
>
>     -- chris
Thomas Hawtin - 27 Oct 2006 14:38 GMT
> 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
tekinico - 28 Oct 2006 06:35 GMT
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
Andrew Thompson - 27 Oct 2006 12:29 GMT
> > 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.