different classpath's

Discussion in 'Java' started by Matthijs Blaas, May 15, 2004.

  1. 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
    Matthijs Blaas, May 15, 2004
    #1
    1. Advertising

  2. Matthijs Blaas

    Oscar kind Guest

    "Matthijs Blaas" <<remthis>thijs_blaas <at>hotmaildotcom> wrote:
    > 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

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, May 16, 2004
    #2
    1. Advertising

  3. "Matthijs Blaas thijs_blaas hotmaildotcom>" <<remthisat> wrote in message
    news:40a5e87b$0$150$...
    > 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.
    Larry Barowski, May 16, 2004
    #3
  4. "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote in message
    news:...
    >
    > 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" <> wrote in message
    news:...
    >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...?
    Matthijs Blaas, May 18, 2004
    #4
  5. On Tue, 18 May 2004 10:20:46 +0200, Matthijs Blaasthijs_blaas wrote:

    > "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote in message
    >> 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...


    You can if you sign it (I would imagine), but..

    > "Oscar kind" <> wrote in message
    >>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...


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

    You can see it in operation thru
    any of the links in the list here..
    <http://www.physci.org/launcher.jsp>

    >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 <param>.

    >..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.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 18, 2004
    #5
  6. 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

    "Andrew Thompson" <> wrote in message
    news:4q5xgh35urxl.pvhpjqs9nyyc$...
    > On Tue, 18 May 2004 10:20:46 +0200, Matthijs Blaasthijs_blaas wrote:
    >
    > > "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote in

    message
    > >> 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...

    >
    > You can if you sign it (I would imagine), but..
    >
    > > "Oscar kind" <> wrote in message
    > >>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...

    >
    > What? Like Launcher does for apps?
    > <http://www.physci.org/codes/display.jsp?fl=%2FLauncher.java#179>
    >
    > You can see it in operation thru
    > any of the links in the list here..
    > <http://www.physci.org/launcher.jsp>
    >
    > >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 <param>.
    >
    > >..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.
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/ Open-source software suite
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.1point1C.org/ Science & Technology
    Matthijs Blaas, May 18, 2004
    #6
  7. [comments to bottom]

    On Tue, 18 May 2004 14:55:28 +0200, Matthijs Blaasthijs_blaas wrote:
    > "Andrew Thompson" <> wrote in message
    >> On Tue, 18 May 2004 10:20:46 +0200, Matthijs Blaasthijs_blaas wrote:

    (instantiate from..)
    >>> applet...

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


    > Yeah exactly like your Launcher does!


    >> You can see it in operation thru
    >> any of the links in the list here..
    >> <http://www.physci.org/launcher.jsp>
    >>
    >>>this applet could be in another directory every time...

    ...
    >> A typical example simply adds the project
    >> directory to the 'archive' attribute..
    >> ...
    >> archive="Launcher.jar, /codes/AWT1/"


    > 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

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 18, 2004
    #7
  8. "Andrew Thompson" <> wrote in message
    news:...
    > [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! ;-)
    Matthijs Blaas, May 18, 2004
    #8
  9. On Tue, 18 May 2004 17:16:00 +0200, Matthijs Blaasthijs_blaas wrote:

    > "Andrew Thompson" <> wrote in message

    ....
    >> 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 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

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 18, 2004
    #9
  10. "Andrew Thompson" <> wrote in message
    news:173cratj9g7sd$.1wjtbp812l0o1$...
    > >> 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 suppose you don't use a jar for your loader applet

    >
    > ...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..?
    Matthijs Blaas, May 18, 2004
    #10
  11. On Tue, 18 May 2004 20:21:21 +0200, Matthijs Blaasthijs_blaas wrote:

    >> If that is the case, the tag should be either..
    >> archive="Loader.jar, ./sub2/" or..
    >> archive="Loader.jar, /applet/sub2/"


    > <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.

    >> ..and put it up as test3.html

    >
    > 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..)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 18, 2004
    #11
  12. "Andrew Thompson" <> wrote in message
    news:...
    > 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
    Matthijs Blaas, May 18, 2004
    #12
  13. On Wed, 19 May 2004 00:18:58 +0200, Matthijs Blaasthijs_blaas wrote:

    > 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.. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 18, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    7,205
    Tony Morris
    Feb 9, 2004
  2. Gabe
    Replies:
    3
    Views:
    1,049
  3. Replies:
    2
    Views:
    718
  4. 525
    Replies:
    5
    Views:
    369
  5. Replies:
    2
    Views:
    1,075
    Mike Schilling
    Mar 2, 2008
Loading...

Share This Page