adding jars to classpath on runtime without -cp option

Discussion in 'Java' started by rony, Nov 28, 2004.

  1. rony

    rony Guest

    Hi all,

    I need to add jar to the classpath on runtime.
    When I say runtime I mean without using -cp option when running the program, but
    dynamically after the program already started.

    I tried to use System.setProperites("java.class.path",newClassPath) but
    It seems there is no effect ( classes are not found ).


    any ideas?

    Thanks a lot.
     
    rony, Nov 28, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Nov 28, 2004
    #2
    1. Advertising

  3. Ferenc Hechler, Nov 28, 2004
    #3
  4. rony

    rony Guest

    Andrew Thompson <> wrote in message news:<geiqvmfi1crk.ug6mnkleaqmh$>...
    > On 28 Nov 2004 06:59:03 -0800, rony wrote:
    >
    > > I need to add jar ..

    >
    > What jar(s)?
    >
    > >..to the classpath on runtime.

    >
    > Why?
    >
    > > ..any ideas?

    >
    > Custom classloader.
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html>


    1. about the why and the jars. I'm writting testing environment, the
    testing environment supply easy api for logging,telnet.... and api for
    other professional tools. the user write the tests in java and I have
    no control on his the code, the user may use log4j for example which
    means I need to add it to my classpath. so you ask your self why I
    dont just add it to the batch file ?
    well, there is code that is version dependent, when I run regression I
    want to use os2.5 and app2.2 which means different jars then the
    current set I work with which is os2.6 ... and I want to read those
    jars from ini file so I can make Ini file for each setup ( I know I
    can do bacth for each setup ).
    That why I want to add jars to my class path after the program already
    up( I hope this is not to complicated ).

    2. I want the VM machine to find those classes the user use. I know
    how to load class that I know it's name in advance.

    any ideas?
     
    rony, Nov 29, 2004
    #4
  5. rony

    rony Guest

    "Ferenc Hechler" <> wrote in message news:<>...
    > You can use URLClassLoader to do the class-loading.
    > Take a look at the following thread:
    > http://forum.java.sun.com/thread.jspa?threadID=300557&start=15&tstart=0
    >
    > bye, feri


    Hi,

    The problem is that I don't know the name of the class that is going
    to be used. I just know that jar where it is located. I want the VM to
    look there.

    Thanks,
    Rony
     
    rony, Nov 29, 2004
    #5
  6. rony

    rony Guest

    "Ferenc Hechler" <> wrote in message news:<>...
    > You can use URLClassLoader to do the class-loading.
    > Take a look at the following thread:
    > http://forum.java.sun.com/thread.jspa?threadID=300557&start=15&tstart=0
    >
    > bye, feri


    Hi,

    The problem is that I don't know the name of the class that is going
    to be used. I just know that jar where it is located. I want the VM to
    look there.

    Thanks,
    Rony
     
    rony, Nov 29, 2004
    #6
  7. On 28 Nov 2004 23:28:22 -0800, rony wrote:

    > I want the VM machine to find those classes the user use. I know
    > how to load class that I know it's name in advance.


    Do a File.list() on the directory. Present the user with a JList
    of jars if more than one is found. Get the enumeration of the
    ZipEntries from the selected Jar and them's your classes.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Nov 29, 2004
    #7
  8. Hi Rony,

    thats exactly what the Class "ClassPathHacker" is doing,
    Perhaps you should look at the thread
    http://forum.java.sun.com/thread.jspa?threadID=300557&start=0&tstart=0
    again.
    There are two solutions mentioned.
    One using reflections to access the protected method addURL of ClassLoader
    and the other changing the Thread-Context ClassLoader using
    Thread.currentThread().setContextClassLoader .

    bye,

    feri


    "rony" <> schrieb im Newsbeitrag
    news:...
    > "Ferenc Hechler" <> wrote in message
    > news:<>...
    >> You can use URLClassLoader to do the class-loading.
    >> Take a look at the following thread:
    >> http://forum.java.sun.com/thread.jspa?threadID=300557&start=15&tstart=0
    >>
    >> bye, feri

    >
    > Hi,
    >
    > The problem is that I don't know the name of the class that is going
    > to be used. I just know that jar where it is located. I want the VM to
    > look there.
    >
    > Thanks,
    > Rony
     
    Ferenc Hechler, Nov 29, 2004
    #8
  9. rony

    rony Guest

    "Ferenc Hechler" <> wrote in message news:<>...
    > Hi Rony,
    >
    > thats exactly what the Class "ClassPathHacker" is doing,
    > Perhaps you should look at the thread
    > http://forum.java.sun.com/thread.jspa?threadID=300557&start=0&tstart=0
    > again.
    > There are two solutions mentioned.
    > One using reflections to access the protected method addURL of ClassLoader
    > and the other changing the Thread-Context ClassLoader using
    > Thread.currentThread().setContextClassLoader .
    >
    > bye,
    >
    > feri
    >
    >
    > "rony" <> schrieb im Newsbeitrag
    > news:...
    > > "Ferenc Hechler" <> wrote in message
    > > news:<>...
    > >> You can use URLClassLoader to do the class-loading.
    > >> Take a look at the following thread:
    > >> http://forum.java.sun.com/thread.jspa?threadID=300557&start=15&tstart=0
    > >>
    > >> bye, feri

    > >
    > > Hi,
    > >
    > > The problem is that I don't know the name of the class that is going
    > > to be used. I just know that jar where it is located. I want the VM to
    > > look there.
    > >
    > > Thanks,
    > > Rony


    Hi,

    an answer at last.

    thanks a lot feri.
     
    rony, Nov 30, 2004
    #9
    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. Ike
    Replies:
    6
    Views:
    1,066
    Andrew Thompson
    Sep 24, 2004
  2. Replies:
    5
    Views:
    9,065
  3. JavaEnquirer
    Replies:
    2
    Views:
    532
    JavaEnquirer
    Feb 22, 2006
  4. Lars Willich
    Replies:
    9
    Views:
    606
    Snyke
    Aug 28, 2007
  5. Ulf Meinhardt
    Replies:
    2
    Views:
    1,412
Loading...

Share This Page