changing the classpath at runtime in code

Discussion in 'Java' started by Aryeh M. Friedman, Oct 25, 2007.

  1. Aryeh M. Friedman, Oct 25, 2007
    #1
    1. Advertising

  2. Aryeh M. Friedman wrote:
    > Is it possible for a java app to change it's classpath at runtime. I
    > tried:
    >
    > System.setProperty("java.class.path","foo")
    >
    > The reason for wanting to try is URLClassLoader will not physically
    > reread a class file if it is in the classpath.


    Create a URLClassLoader with a url that are *not* in the parent
    classloaders url's.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 25, 2007
    #2
    1. Advertising

  3. On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    > Aryeh M. Friedman wrote:
    > > Is it possible for a java app to change it's classpath at runtime. I
    > > tried:

    >
    > > System.setProperty("java.class.path","foo")

    >
    > > The reason for wanting to try is URLClassLoader will not physically
    > > reread a class file if it is in the classpath.

    >
    > Create a URLClassLoader with a url that are *not* in the parent
    > classloaders url's.


    so just do:

    URL[] url={new URL("file://foo")};
    URLClassLoader laoder=new URLClassLoader(url);
    ?
     
    Aryeh M. Friedman, Oct 25, 2007
    #3
  4. Aryeh M. Friedman wrote:
    > On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    >> Aryeh M. Friedman wrote:
    >>> Is it possible for a java app to change it's classpath at runtime. I
    >>> tried:
    >>> System.setProperty("java.class.path","foo")
    >>> The reason for wanting to try is URLClassLoader will not physically
    >>> reread a class file if it is in the classpath.

    >> Create a URLClassLoader with a url that are *not* in the parent
    >> classloaders url's.

    >
    > so just do:
    >
    > URL[] url={new URL("file://foo")};
    > URLClassLoader laoder=new URLClassLoader(url);
    > ?


    I have a little demo example I often use to illustrate
    (subdir test is not in classpath of program).

    Arne

    ===============================

    import java.io.*;
    import java.net.*;

    public class DoubleDynmaic {
    private static void dynno(int n) {
    (new File("test")).mkdir();
    try {
    OutputStream os = new FileOutputStream("test/Test.java");
    PrintStream ps = new PrintStream(os);
    ps.println("public class Test {");
    ps.println(" public Test() {");
    ps.println(" System.out.println(" + n + ");");
    ps.println(" }");
    ps.println("}");
    ps.close();
    os.close();
    Runtime.getRuntime().exec("javac -d test
    test/Test.java").waitFor();
    URL[] url = new URL[1];
    url[0] = new URL("file:test/");
    URLClassLoader cl = new URLClassLoader(url);
    Class.forName("Test", true, cl).newInstance();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    for(int i = 0; i < 10; i++) {
    dynno(i);
    }
    }
    }
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 25, 2007
    #4
  5. On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    > Aryeh M. Friedman wrote:
    > > On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    > >> Aryeh M. Friedman wrote:
    > >>> Is it possible for a java app to change it's classpath at runtime. I
    > >>> tried:
    > >>> System.setProperty("java.class.path","foo")
    > >>> The reason for wanting to try is URLClassLoader will not physically
    > >>> reread a class file if it is in the classpath.
    > >> Create a URLClassLoader with a url that are *not* in the parent
    > >> classloaders url's.

    >
    > > so just do:

    >
    > > URL[] url={new URL("file://foo")};
    > > URLClassLoader laoder=new URLClassLoader(url);
    > > ?

    >
    > I have a little demo example I often use to illustrate
    > (subdir test is not in classpath of program).


    You posted this in the original thread but as noted in the linked to
    thread almost all the classes I am dealing with *WILL* be in the
    classpath.... so the question remains if I do the code in my previous
    post will it now load from the class path or from "foo".

    >
    > Arne
    >
    > ===============================
    >
    > import java.io.*;
    > import java.net.*;
    >
    > public class DoubleDynmaic {
    > private static void dynno(int n) {
    > (new File("test")).mkdir();
    > try {
    > OutputStream os = new FileOutputStream("test/Test.java");
    > PrintStream ps = new PrintStream(os);
    > ps.println("public class Test {");
    > ps.println(" public Test() {");
    > ps.println(" System.out.println(" + n + ");");
    > ps.println(" }");
    > ps.println("}");
    > ps.close();
    > os.close();
    > Runtime.getRuntime().exec("javac -d test
    > test/Test.java").waitFor();
    > URL[] url = new URL[1];
    > url[0] = new URL("file:test/");
    > URLClassLoader cl = new URLClassLoader(url);
    > Class.forName("Test", true, cl).newInstance();
    > } catch (Exception e) {
    > e.printStackTrace();
    > }
    > }
    > public static void main(String[] args) {
    > for(int i = 0; i < 10; i++) {
    > dynno(i);
    > }
    > }
    >
    > }
     
    Aryeh M. Friedman, Oct 25, 2007
    #5
  6. On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    > Aryeh M. Friedman wrote:
    > > On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    > >> Aryeh M. Friedman wrote:
    > >>> Is it possible for a java app to change it's classpath at runtime. I
    > >>> tried:
    > >>> System.setProperty("java.class.path","foo")
    > >>> The reason for wanting to try is URLClassLoader will not physically
    > >>> reread a class file if it is in the classpath.
    > >> Create a URLClassLoader with a url that are *not* in the parent
    > >> classloaders url's.

    >
    > > so just do:

    >
    > > URL[] url={new URL("file://foo")};
    > > URLClassLoader laoder=new URLClassLoader(url);
    > > ?

    >
    > I have a little demo example I often use to illustrate
    > (subdir test is not in classpath of program).


    Perhaps google ate my previous response.... but as I said several
    places (including the linked to thread) almost all my classes *WILL*
    be in the classpath... btw as mentioned in an other thread your demo
    doesn't quite work once divorced from the code to rewrite the class
    (see orginial thread)... so the question remains does the code above
    load from "foo" or from the classpath when I do loader.loadClass(...)
     
    Aryeh M. Friedman, Oct 25, 2007
    #6
  7. Aryeh M. Friedman wrote:
    > On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    >> Aryeh M. Friedman wrote:
    >>> On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    >>>> Aryeh M. Friedman wrote:
    >>>>> Is it possible for a java app to change it's classpath at runtime. I
    >>>>> tried:
    >>>>> System.setProperty("java.class.path","foo")
    >>>>> The reason for wanting to try is URLClassLoader will not physically
    >>>>> reread a class file if it is in the classpath.
    >>>> Create a URLClassLoader with a url that are *not* in the parent
    >>>> classloaders url's.
    >>> so just do:
    >>> URL[] url={new URL("file://foo")};
    >>> URLClassLoader laoder=new URLClassLoader(url);
    >>> ?

    >> I have a little demo example I often use to illustrate
    >> (subdir test is not in classpath of program).

    >
    > You posted this in the original thread but as noted in the linked to
    > thread almost all the classes I am dealing with *WILL* be in the
    > classpath.... so the question remains if I do the code in my previous
    > post will it now load from the class path or from "foo".


    If the class get loaded by the parent classloader because it is
    in the apps general classpath, then you can (as far as I know)
    neither get it unloaded or reloaded.

    You need to get those classes of the classpath.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 25, 2007
    #7
  8. Aryeh M. Friedman

    Daniel Pitts Guest

    Aryeh M. Friedman wrote:
    > Is it possible for a java app to change it's classpath at runtime. I
    > tried:
    >
    > System.setProperty("java.class.path","foo")
    >
    > The reason for wanting to try is URLClassLoader will not physically
    > reread a class file if it is in the classpath. See
    > http://groups.google.com/group/comp...146df18365a/ceaa154348f6f271#ceaa154348f6f271
    >

    Once a class is loaded by any class loader, it can not be reloaded by
    any other class loader in that same branch of the class-loader tree.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 25, 2007
    #8
  9. Aryeh M. Friedman

    Daniel Pitts Guest

    Aryeh M. Friedman wrote:
    > On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    >> Aryeh M. Friedman wrote:
    >>> On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    >>>> Aryeh M. Friedman wrote:
    >>>>> Is it possible for a java app to change it's classpath at runtime. I
    >>>>> tried:
    >>>>> System.setProperty("java.class.path","foo")
    >>>>> The reason for wanting to try is URLClassLoader will not physically
    >>>>> reread a class file if it is in the classpath.
    >>>> Create a URLClassLoader with a url that are *not* in the parent
    >>>> classloaders url's.
    >>> so just do:
    >>> URL[] url={new URL("file://foo")};
    >>> URLClassLoader laoder=new URLClassLoader(url);
    >>> ?

    >> I have a little demo example I often use to illustrate
    >> (subdir test is not in classpath of program).

    >
    > Perhaps google ate my previous response.... but as I said several
    > places (including the linked to thread) almost all my classes *WILL*
    > be in the classpath... btw as mentioned in an other thread your demo
    > doesn't quite work once divorced from the code to rewrite the class
    > (see orginial thread)... so the question remains does the code above
    > load from "foo" or from the classpath when I do loader.loadClass(...)
    >

    Here's the scoop.
    If its ever on the class path, you can't reload it. What you're trying
    to do is not possible to do without restricting what's on the class
    path. That is not a bad restriction! Just make it happen.

    Changing the class path after the fact isn't going to help either.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 25, 2007
    #9
  10. On Oct 24, 9:31 pm, Arne Vajhøj <> wrote:
    > Aryeh M. Friedman wrote:
    > > On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    > >> Aryeh M. Friedman wrote:
    > >>> On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    > >>>> Aryeh M. Friedman wrote:
    > >>>>> Is it possible for a java app to change it's classpath at runtime. I
    > >>>>> tried:
    > >>>>> System.setProperty("java.class.path","foo")
    > >>>>> The reason for wanting to try is URLClassLoader will not physically
    > >>>>> reread a class file if it is in the classpath.
    > >>>> Create a URLClassLoader with a url that are *not* in the parent
    > >>>> classloaders url's.
    > >>> so just do:
    > >>> URL[] url={new URL("file://foo")};
    > >>> URLClassLoader laoder=new URLClassLoader(url);
    > >>> ?
    > >> I have a little demo example I often use to illustrate
    > >> (subdir test is not in classpath of program).

    >
    > > You posted this in the original thread but as noted in the linked to
    > > thread almost all the classes I am dealing with *WILL* be in the
    > > classpath.... so the question remains if I do the code in my previous
    > > post will it now load from the class path or from "foo".

    >
    > If the class get loaded by the parent classloader because it is
    > in the apps general classpath, then you can (as far as I know)
    > neither get it unloaded or reloaded.
    >
    > You need to get those classes of the classpath.


    Since this is for a commercial unit testing product this is not
    possible in practice (it is not safe to make assumptions about where
    people place things). Some one off line suggested forking a process
    to do it.... if I can't find a ClassLoader based solution I will have
    to do that but it is down right a) not portable b) evil
     
    Aryeh M. Friedman, Oct 25, 2007
    #10
  11. Aryeh M. Friedman

    Daniel Pitts Guest

    Aryeh M. Friedman wrote:
    > On Oct 24, 9:31 pm, Arne Vajhøj <> wrote:
    >> Aryeh M. Friedman wrote:
    >>> On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    >>>> Aryeh M. Friedman wrote:
    >>>>> On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    >>>>>> Aryeh M. Friedman wrote:
    >>>>>>> Is it possible for a java app to change it's classpath at runtime. I
    >>>>>>> tried:
    >>>>>>> System.setProperty("java.class.path","foo")
    >>>>>>> The reason for wanting to try is URLClassLoader will not physically
    >>>>>>> reread a class file if it is in the classpath.
    >>>>>> Create a URLClassLoader with a url that are *not* in the parent
    >>>>>> classloaders url's.
    >>>>> so just do:
    >>>>> URL[] url={new URL("file://foo")};
    >>>>> URLClassLoader laoder=new URLClassLoader(url);
    >>>>> ?
    >>>> I have a little demo example I often use to illustrate
    >>>> (subdir test is not in classpath of program).
    >>> You posted this in the original thread but as noted in the linked to
    >>> thread almost all the classes I am dealing with *WILL* be in the
    >>> classpath.... so the question remains if I do the code in my previous
    >>> post will it now load from the class path or from "foo".

    >> If the class get loaded by the parent classloader because it is
    >> in the apps general classpath, then you can (as far as I know)
    >> neither get it unloaded or reloaded.
    >>
    >> You need to get those classes of the classpath.

    >
    > Since this is for a commercial unit testing product this is not
    > possible in practice (it is not safe to make assumptions about where
    > people place things). Some one off line suggested forking a process
    > to do it.... if I can't find a ClassLoader based solution I will have
    > to do that but it is down right a) not portable b) evil
    >
    >

    Hmm, I think as a publisher of said software, you can say how it should
    work. If you can't make any assumptions whatsoever then you're lost.
    You'll end up writing this program for clients who want to be able to
    bounce a network signal off the moon for some reason.

    It's all about making reasonable assumptions. It is reasonable to
    assume that you can have a degree of control the environment/context in
    which your application starts up. Document the fact that the code to be
    tested can't be in the same path as your product.

    Also, you seem to make a big deal about this code being "commercial".
    This rule applies for any publicly distributed product, whether
    commercial or otherwise.

    Honestly, I think you're going down the wrong road with this. Have you
    looked at other existing test frameworks? What is yours going to offer
    that, say, JUnit doesn't?



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Oct 25, 2007
    #11
  12. Aryeh M. Friedman

    Roedy Green Guest

    On Wed, 24 Oct 2007 23:38:10 -0000, "Aryeh M. Friedman"
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Is it possible for a java app to change it's classpath at runtime


    yes but it requires firing up a new classloader.

    See http://mindprod.com/jgloss/classloader.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 25, 2007
    #12
  13. Aryeh M. Friedman wrote:
    > On Oct 24, 9:31 pm, Arne Vajhøj <> wrote:
    >> Aryeh M. Friedman wrote:
    >>> On Oct 24, 9:13 pm, Arne Vajhøj <> wrote:
    >>>> Aryeh M. Friedman wrote:
    >>>>> On Oct 24, 8:16 pm, Arne Vajhøj <> wrote:
    >>>>>> Aryeh M. Friedman wrote:
    >>>>>>> Is it possible for a java app to change it's classpath at runtime. I
    >>>>>>> tried:
    >>>>>>> System.setProperty("java.class.path","foo")
    >>>>>>> The reason for wanting to try is URLClassLoader will not physically
    >>>>>>> reread a class file if it is in the classpath.
    >>>>>> Create a URLClassLoader with a url that are *not* in the parent
    >>>>>> classloaders url's.
    >>>>> so just do:
    >>>>> URL[] url={new URL("file://foo")};
    >>>>> URLClassLoader laoder=new URLClassLoader(url);
    >>>>> ?
    >>>> I have a little demo example I often use to illustrate
    >>>> (subdir test is not in classpath of program).
    >>> You posted this in the original thread but as noted in the linked to
    >>> thread almost all the classes I am dealing with *WILL* be in the
    >>> classpath.... so the question remains if I do the code in my previous
    >>> post will it now load from the class path or from "foo".

    >> If the class get loaded by the parent classloader because it is
    >> in the apps general classpath, then you can (as far as I know)
    >> neither get it unloaded or reloaded.
    >>
    >> You need to get those classes of the classpath.

    >
    > Since this is for a commercial unit testing product this is not
    > possible in practice (it is not safe to make assumptions about where
    > people place things). Some one off line suggested forking a process
    > to do it.... if I can't find a ClassLoader based solution I will have
    > to do that but it is down right a) not portable b) evil


    You do not have any control over where people put their stuff,
    but you have control over your stuff.

    If the only thing in classpath is your jar file, then you can
    create your own classloader for their stuff and unload and reload
    as needed.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 27, 2007
    #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. Brian Andersen

    Changing the Classpath at runtime

    Brian Andersen, Dec 19, 2003, in forum: Java
    Replies:
    8
    Views:
    13,102
    Real Gagnon
    Dec 19, 2003
  2. Flip
    Replies:
    3
    Views:
    7,417
    Tony Morris
    Feb 9, 2004
  3. Gabe
    Replies:
    3
    Views:
    1,122
  4. Replies:
    2
    Views:
    811
  5. Replies:
    2
    Views:
    1,161
    Mike Schilling
    Mar 2, 2008
Loading...

Share This Page