setting up CLASS PATH dynamically

Discussion in 'Java' started by strus_82, Mar 29, 2008.

  1. strus_82

    strus_82 Guest

    Hello,
    I have a question related to CLASS PATH - is there a possibility to
    add some path to CLASS PATH during a runtime of application?

    I've tried the following:

    Properties properties = System.getProperties();
    String key = "java.class.path";
    String value = "d:\\some\\new\\path\\;" + properties.getProperty(key);
    System.setProperty("java.class.path", value);

    and then using a reflection mechanism I've tried to execute some
    method but it doesn't work properly (seems like the class path is not
    updated).

    When I set the CLASS PATH during the execution of program the method
    works fine.


    thanks in advance,
    M.
     
    strus_82, Mar 29, 2008
    #1
    1. Advertising

  2. strus_82

    Arne Vajhøj Guest

    strus_82 wrote:
    > I have a question related to CLASS PATH - is there a possibility to
    > add some path to CLASS PATH during a runtime of application?
    >
    > I've tried the following:
    >
    > Properties properties = System.getProperties();
    > String key = "java.class.path";
    > String value = "d:\\some\\new\\path\\;" + properties.getProperty(key);
    > System.setProperty("java.class.path", value);
    >
    > and then using a reflection mechanism I've tried to execute some
    > method but it doesn't work properly (seems like the class path is not
    > updated).
    >
    > When I set the CLASS PATH during the execution of program the method
    > works fine.


    I don't think so.

    Create an URLClassLoader with the classpath you need and use
    that to load the classes.

    Arne
     
    Arne Vajhøj, Mar 29, 2008
    #2
    1. Advertising

  3. strus_82

    Roedy Green Guest

    On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I have a question related to CLASS PATH - is there a possibility to
    >add some path to CLASS PATH during a runtime of application?


    you can fire up a new classloader with a different classpath.
    see http://mindprod.com/jgloss/classloader.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 30, 2008
    #3
  4. strus_82

    strus_82 Guest

    On 30 Mar, 09:22, Roedy Green <>
    wrote:
    > On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > >I have a question related to CLASS PATH - is there a possibility to
    > >add some path to CLASS PATH during a runtime of application?

    >
    > you can fire up a new classloader with a different classpath.
    > seehttp://mindprod.com/jgloss/classloader.html
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Hello,
    I'm trying to write my ClassLoader but I have the following problem:
    when I read all data from .class (here it is CustomClassLoader.class)
    file to byte array - I call defineClass() but an exception is thrown:
    "d:/examples/temp/CustomClassLoader (wrong name: CustomClassLoader)".

    I think I pass wrong first parameter to defineClass(String name,
    byte[] b, int off, int len). The CustomClassLoader.class file is
    located in d:\examples\temp\.

    I have also tried to use findClass() but it's the same.

    I have tried different combination of the path to that .class file but
    the result is the same all the time :/

    Could You give me some hint? All I want to do (for couple hours :/) is
    to load class from some path of the local file system (but the path is
    not set in CLASS PATH).

    Thanks in advance,
    M.
     
    strus_82, Mar 30, 2008
    #4
  5. strus_82

    Arne Vajhøj Guest

    strus_82 wrote:
    > I'm trying to write my ClassLoader but I have the following problem:
    > when I read all data from .class (here it is CustomClassLoader.class)
    > file to byte array - I call defineClass() but an exception is thrown:
    > "d:/examples/temp/CustomClassLoader (wrong name: CustomClassLoader)".
    >
    > I think I pass wrong first parameter to defineClass(String name,
    > byte[] b, int off, int len). The CustomClassLoader.class file is
    > located in d:\examples\temp\.
    >
    > I have also tried to use findClass() but it's the same.
    >
    > I have tried different combination of the path to that .class file but
    > the result is the same all the time :/
    >
    > Could You give me some hint? All I want to do (for couple hours :/) is
    > to load class from some path of the local file system (but the path is
    > not set in CLASS PATH).


    It is much simpler than that !

    URLClassLoader cl = new URLClassLoader("file:/d:/examples/temp/");
    Object o = Class.forName("mypackage.MyClass", true, cl).newInstance();

    will load mypackage.MyClass from D:\examples\temp\mypackage\MyClass.class !

    Arne
     
    Arne Vajhøj, Mar 30, 2008
    #5
  6. strus_82

    strus_82 Guest


    > It is much simpler than that !
    >
    > URLClassLoader cl = new URLClassLoader("file:/d:/examples/temp/");
    > Object o = Class.forName("mypackage.MyClass", true, cl).newInstance();
    >
    > will load mypackage.MyClass from D:\examples\temp\mypackage\MyClass.class !
    >
    > Arne


    Indeed :) thx. I've tried this but I used toURL() as a argument to
    URLClassLoader (it takes URL[]) - but this toURL() is deprecated
    starting from v.1.5, bur as You wrote I can use simply a string:
    URLClassLoader(new URL[] {new URL("file:/d:/examples/temp/")});

    But could you tell me what I did wrong in case of defining my
    ClassLoader?

    Best regards,
    M.
     
    strus_82, Mar 30, 2008
    #6
    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. Wolfgang Kaml
    Replies:
    11
    Views:
    4,333
    Kevin Buchan
    Jan 22, 2004
  2. Replies:
    10
    Views:
    3,826
  3. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  4. Terracotta

    library.path VS class.path

    Terracotta, Jul 16, 2007, in forum: Java
    Replies:
    1
    Views:
    1,019
    Gordon Beaton
    Jul 16, 2007
  5. Dave
    Replies:
    1
    Views:
    153
    Yuan Ren[MSFT]
    Dec 5, 2005
Loading...

Share This Page