java - 动态修改类路径

标签 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);

最佳答案

我成功了

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

关于java - 动态修改类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15007794/

相关文章:

java - 无法弄清楚 flattnes 参数在 PathIterator 中的作用?

java - 如何在 Glassfish (2.x) 中设置 RMI,以便注入(inject)远程 EJB

java - 在 Mac OS 中将 JAR 添加到 CLASSPATH

java - -sourcepath 与 -classpath

java - 一个类在 JVM 中占用多少空间?

Java Class.isAssignableFrom 总是返回 false ... 仅在 IDE 之外

java - 如何使用 Dataflow 多次执行相同的工作?

classpath - Java 类路径通配符行为

java - Java中如何处理冲突的依赖关系

java - JTextArea 中 setText() 的机制?