Tuesday, 17 May 2016

How to add jar into classpath at runtime?

  // pathToJar -> /path/to/jar/MyJarName.jar

  public void addToClassPath(String pathToJar) throws Exception {
    File file = new File(pathToJar);
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(ClassLoader.getSystemClassLoader(), file.toURI().toURL());
  }

No comments:

Post a Comment