different classpath's

M

Matthijs Blaas

Hi!

I have an applet that loaded another class from the same classpath:

Class clas = Class.forName("Classfile");
sub = (Applet)clas.newInstance();

Now I want to load a class file thats not in the same directory, the only
way I'm able to do this is by making a urlconnection to the class file,
retrieving all the bytes from this classfile using a getInputStream and then
define the class file using the defineClass method.

Is there another way to instantiate class files not in the same
directory/url than the instantiating applet? And how could this be done for
jar files?

Thanks in advance!

Matthijs
 
O

Oscar kind

Matthijs Blaas said:
I have an applet that loaded another class from the same classpath:

Class clas = Class.forName("Classfile");
sub = (Applet)clas.newInstance();

This is IMHO the preferred way. It always works when the class to load is
on the classpath.

Now I want to load a class file thats not in the same directory, the only
way I'm able to do this is by making a urlconnection to the class file,
retrieving all the bytes from this classfile using a getInputStream and then
define the class file using the defineClass method.

Is there another way to instantiate class files not in the same
directory/url than the instantiating applet? And how could this be done for
jar files?

Yes: add that directory to the classpath. For .jar files, use the
Classpath header in the manifest. See "Download Extensions" at:
http://java.sun.com/docs/books/tutorial/jar/basics/manifest.html


Oscar
 
L

Larry Barowski

Matthijs Blaas thijs_blaas hotmaildotcom> said:
Hi!

I have an applet that loaded another class from the same classpath:

Class clas = Class.forName("Classfile");
sub = (Applet)clas.newInstance();

Now I want to load a class file thats not in the same directory, the only
way I'm able to do this is by making a urlconnection to the class file,
retrieving all the bytes from this classfile using a getInputStream and then
define the class file using the defineClass method.

Is there another way to instantiate class files not in the same
directory/url than the instantiating applet? And how could this be done for
jar files?

You can probably use a URLClassLoader.
 
M

Matthijs Blaas

Larry Barowski said:
You can probably use a URLClassLoader.

Hmm, tried that but i think that because of security restrictions applets
can't use a URLClassLoader... or maybe I did something wrong...

Oscar kind said:
Yes: add that directory to the classpath. For .jar files, use the
Classpath header in the manifest. See "Download Extensions" at:
http://java.sun.com/docs/books/tutorial/jar/basics/manifest.html

That would be possible, but I use a classfile that loads an applet in a
dynamic way, it reads the param name="applet" tag i feed it and loads that
applet... this applet could be in another directory every time... so I guess
I should stick to reading the class file over an urlconnection & define the
class after i've read all bytes...?
 
A

Andrew Thompson

....
Hmm, tried that but i think that because of security restrictions applets
can't use a URLClassLoader... or maybe I did something wrong...

You can if you sign it (I would imagine), but..
That would be possible, but I use a classfile that loads an applet in a
dynamic way, it reads the param name="applet" tag i feed it and loads that
applet...

What? Like Launcher does for apps?
<http://www.physci.org/codes/display.jsp?fl=/Launcher.java#179>

You can see it in operation thru
any of the links in the list here..
this applet could be in another directory every time...

My little projects are at various
locations about the site.

A typical example simply adds the project
directory to the 'archive' attribute..

....
archive="Launcher.jar, /codes/AWT1/"
code="Launcher.class"
....
height=140>
<param name=class value="SimpleGUI1">
....

... and adds the name as a said:
..so I guess
I should stick to reading the class file over an urlconnection & define the
class after i've read all bytes...?

Either of the two solutions discussed
thus far should be suitable for what
you wish to achieve. So far as I can see.
 
M

Matthijs Blaas

Yeah exactly like your Launcher does! I pass the path to the loader class
using a param name=path, I also have a param name=class for the desired
class name to instantiate... this gives:
Class clas = Class.forName(path+title);
sub = (Applet)clas.newInstance();

But this doesn't work...? I thought it was impossible to have the forName
load from another path as the loader class. Your loader seems to do this,
however i can't see how... you save the prefix path and the class name in
one variable which should give: Class clas = Class.forName(path+title);
which doesn't work for me?

-Thijs
 
A

Andrew Thompson

[comments to bottom]

Yeah exactly like your Launcher does!
I pass the path to the loader class
using a param name=path, I also have a param name=class for the desired
class name to instantiate...

No. Notice how I add the path to my app to
the *archive* parameter (path is '/codes/AWT1/').
_that_ is where you need to add your 'path'
parameter..

[ And could I ask you to 'in-line post' with
trimming as I have done above? It makes it
much easier to read threads.
<http://www.physci.org/codes/javafaq.jsp#netiquette> ]

HTH
 
M

Matthijs Blaas

Andrew Thompson said:
[comments to bottom]

No. Notice how I add the path to my app to
the *archive* parameter (path is '/codes/AWT1/').
_that_ is where you need to add your 'path'
parameter..

I didn't know you could set a classpath with the archive tag, I thought it
was only to point to the JAR archive.. Anyway, I dont seem to get it to work
in my case 8-(
I suppose you don't use a jar for your loader applet and only use it to
point to the right path for the other class files to be found... I've put
some stripped down examples online which show my problem:
http://www.virtual-boy.org/applet/
The first example is how it does work, in the second example i try to use
the archive tag, creating a jar archive for the Loader classes didn't change
anything & I couldn't find a jar for your loader applet so I suspected that
I only needed a class file for the loader...
[ And could I ask you to 'in-line post' with
trimming as I have done above? It makes it
much easier to read threads.
<http://www.physci.org/codes/javafaq.jsp#netiquette> ]

HTH

Will do! ;-)
 
A

Andrew Thompson

....
I suppose you don't use a jar for your loader applet

....Yeah I do. 'Launcher.jar'
..and only use it to
point to the right path for the other class files to be found...

!! No !! Here is that tag again..
archive="Launcher.jar, /codes/AWT1/"
..I've put
some stripped down examples online which show my problem:
http://www.virtual-boy.org/applet/

*Finally* ;-)

OK.. following..
<http://www.virtual-boy.org/applet/test2.html>
That's the one of interest?

It has a tag..
archive="Loader.jar, \sub2\"

1) note that you use '\' ..very windows,
try '/' (as above, in my example)

2) Where is <http://www.virtual-boy.org/sub2/>?
It returns a 404, I suspect you *actually* need
<http://www.virtual-boy.org/applet/sub2/>
(I got 403 on that)

If that is the case, the tag should be either..
archive="Loader.jar, ./sub2/" or..
archive="Loader.jar, /applet/sub2/"
Either of those archive tags should
point to the 2nd directory.

Give that a go, and put
it up as test3.html

HTH
 
M

Matthijs Blaas

Andrew Thompson said:
...Yeah I do. 'Launcher.jar'

Ok, i've put the loader class files in a jar file too...
OK.. following..
<http://www.virtual-boy.org/applet/test2.html>
That's the one of interest?
Yup

It has a tag..
archive="Loader.jar, \sub2\"

1) note that you use '\' ..very windows,
try '/' (as above, in my example)

2) Where is <http://www.virtual-boy.org/sub2/>?
It returns a 404, I suspect you *actually* need
<http://www.virtual-boy.org/applet/sub2/>
(I got 403 on that)

Whoops, it had to be applet/sub2, changed it in the html tag now
If that is the case, the tag should be either..
archive="Loader.jar, ./sub2/" or..
archive="Loader.jar, /applet/sub2/"
Either of those archive tags should
point to the 2nd directory.

Give that a go, and put
it up as test3.html

Ok, i've changed test2.html as it was supposed to be:
<APPLET archive="Loader.jar, /applet/sub2" codebase="/applet/loader/"
code="Loader.class" width="400" height="400" name="Loader">
<param name="name" value="Test"></param>
</APPLET>

The loader jar is loaded from the /applet/loader dir and the subclass should
be loaded from the /applet/sub2 dir... it still doesn't work though... says
it can't find Test.class which is located in /applet/sub2..?
 
A

Andrew Thompson

<APPLET archive="Loader.jar, /applet/sub2" codebase="/applet/loader/"
code="Loader.class" width="400" height="400" name="Loader">
<param name="name" value="Test"></param>
</APPLET>

OooooK... Now look very, ..very
closely at the spec. for your
'/applet/sub2' and note how it
matches neither of the suggestions
I made.
Ok, i've changed test2.html as it was supposed to be:

I have found it is best with web pages that
are being debugged not to alter exiting pages,
but to upload a new page, for easy comparison.

That is why I asked you to upload it as test3.html

Could you upload a test3 with the
archive specified *exactly* as..

archive="Loader.jar, /applet/sub2/"

BTW - do you know how to find the Java
Console? At the moment your 'test2' is showing

java.lang.ClassNotFoundException: Test
.....
Caused by: java.io.IOException: open HTTP connection failed.

What is your browser/OS?

And what is with the 'forbidden' status
of that sub2 directory? If you cannot
get it to work with test3, make a 'test4'
and try it using a copy of the 'Test'
class in a non-forbidden directory
(I do not _think_ that is the
problem, but..)
 
M

Matthijs Blaas

Andrew Thompson said:
And what is with the 'forbidden' status
of that sub2 directory? If you cannot
get it to work with test3, make a 'test4'
and try it using a copy of the 'Test'
class in a non-forbidden directory
(I do not _think_ that is the
problem, but..)

Don't know about the access rights on the server, it's not my server... I
only have rights to upload files and create directories. However when i run
the test3.html from my localhost where i do have all the rights, it still
throws me that classnotfoundexception...
OooooK... Now look very, ..very
closely at the spec. for your
'/applet/sub2' and note how it
matches neither of the suggestions
I made.

heh I'm ashamed of myself! Didn't note that last slash... 8-/
It works perfect now! Thank you very much!!
This is an much easier solution as I originally had worked out, using a
custom classloader, which required me to sign the loader applet etc...
BTW: Do you got any expirience with loading / instantiating jar files this
way (using a Loader applet)?

Anyway thanks for your patience & for helping me out!

-Thijs
 
A

Andrew Thompson

It works perfect now! Thank you very much!!

:) You are welcome, glad to
hear you got there.
This is an much easier solution as I originally had worked out, using a
custom classloader, which required me to sign the loader applet etc...
BTW: Do you got any expirience with loading / instantiating jar files this
way (using a Loader applet)?

You mean by ClassLoader, no.

I have signed JWS apps., but not applets.

I have also played with a simple URLClassLoader
just recently (in an app.), they are surprisingly
simple. If you wanted to experiment for yourself,
I strongly recommend doing it in an application
first, apps. are easier to debug and you can
delay the effort of signing the jar (until
the applet, that is)

It would actually not be that difficult
(so far as I can see from my experiments)
to implement a suitable URLClassLoader for
the functionality you wanted. In fact if
I have it right, the URLClassLoader should
be able to get classes from completely
different domains as well. But (shrugs)
this way works just fine for what I
immediately need.. ;-)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top