动态修改类路径

时间:2013-02-21 16:48:59

标签: java classpath classloader urlclassloader

我想在运行时将/home/nameuser/.myProject/conf下的配置文件添加到类路径中。

我尝试使用URLClassLoader,但没有成功。

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

ClassLoader currentThreadClassLoader
 = Thread.currentThread().getContextClassLoader();

// Add the conf dir to the classpath
// Chain the current thread classloader
URLClassLoader urlClassLoader
 = new URLClassLoader(new URL[]{new File("mtFile").toURL()},
                      currentThreadClassLoader);

// Replace the thread classloader - assumes
// you have permissions to do so
Thread.currentThread().setContextClassLoader(urlClassLoader);

1 个答案:

答案 0 :(得分:0)

我成功了

感谢Darcy Qiu很好URL[] url={new URL("file:///home/nameuser/.myProject/conf/")};我必须做而不是URL[] url={new URL("file:///home/nameuser/.myProject/conf/myfile")};