提问者:小点点

如何将新的jar文件动态加载到JavaVM


我已经创建了javaVM C对象,这是我的库提供的,但是我需要在我的应用程序中添加更多的jar和Java文件。

我能做什么?

图书馆使用JNI_CreateJavaVM(

有人请告诉我是否有任何函数可以将java对象动态加载到已经创建的JavaVM对象中。


共1个答案

匿名用户

如果我理解你的问题。。。

Is there any function to dynamically add java objects to a running JVM?

然后使用以下内容向您的应用程序动态添加JAR。(根据需要添加错误检查和异常处理)

public synchronized void addJarToClassPath(final File jarFile) {
    final String methodName = "addURL";
    final Class<?>[] methodTypes = new Class[] { URL.class };
    try {
        final Method method = URLClassLoader.class.getDeclaredMethod(methodName, methodTypes);
        method.setAccessible(true);
        method.invoke((URLClassLoader) ClassLoader.getSystemClassLoader(), new Object[] { jarFile.toURI().toURL() });
    } catch (final Exception e) {
        throw new RuntimeException("Failed to load " + jarFile.getAbsolutePath(), e);
    }
}