java - 创建新的 ClassLoader 以重新加载 Class

我想在每次调用我的方法时创建一个新的 ClassLoader

所以我可以在不退出程序的情况下重新加载 class

如何更新由 ClassLoader 加载的 class 的方法也是一种解决方案。

我怎样才能做到这一点?

最佳答案

我在这里找到了一个很好的解释性答案:

http://www.exampledepot.com/egs/java.lang/reloadclass.html

重要的是有两个二进制文件夹,在我的例子中: 一个用于测试用例,一个用于程序源。

引用:

URL[] urls = null;
try {
    // Convert the file object to a URL
    File dir = new File(System.getProperty("user.dir")
        +File.separator+"dir"+File.separator);
    URL url = dir.toURL();        // file:/c:/almanac1.4/examples/
    urls = new URL[]{url};
} catch (MalformedURLException e) {
}

try {
    // Create a new class loader with the directory
    ClassLoader cl = new URLClassLoader(urls);

    // Load in the class
    Class cls = cl.loadClass("MyReloadableClassImpl");

https://stackoverflow.com/questions/9819318/

相关文章:

build - TeamCity 工件;排除单个文件

build - 使用 IntelliJ 进行远程构建

unit-testing - 如何从 Go 测试覆盖中忽略生成的文件

eclipse - 如何查看 Eclipse 中触发 "Build workspace"的原因

javascript - 为 JavaScript 构建过程工具

c# - TeamCity 8 中的 DotCover 不起作用

node.js - 用 postcss 观察多个 css 文件并输出一个 bundle.css

c# - 如何在夜间构建期间自动设置程序集版本?

git - 如何将修订和构建日期添加到源?

tfs - 让 TFS 将变更集放入程序集版本中