代码之家  ›  专栏  ›  技术社区  ›  Rogach

是否可以在运行时加载库?

  •  3
  • Rogach  · 技术社区  · 14 年前

    如果java库(.jar文件)不在类路径上,是否可以在运行时加载它?

    1 回复  |  直到 12 年前
        1
  •  6
  •   Community datashaman    7 年前
    URLClassLoader child = new URLClassLoader (myJar.toURL(), this.getClass().getClassLoader());
    Class classToLoad = Class.forName ("com.MyClass", true, child);
    Method method = classToLoad.getDeclaredMethod ("myMethod");
    Object instance = classToLoad.newInstance ();
    Object result = method.invoke (instance);
    

    资料来源: How should I load Jars dynamically at runtime?