1

我有一个正在运行和测试过的代码,我可以在其中加载一个 jar 文件和类路径的所有依赖项。

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;


public URLClassLoader loadjarFile(String path) throws IOException {

        URLClassLoader sysLoader;
        URL u = null;

        try {

            File file = new File("D:\\test\\mytest.jar");
            u = file.toURI().toURL();
            sysLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();

            Class sysclass = URLClassLoader.class;
            Class[] parameters = new Class[] { URL.class };
            Method method = sysclass.getDeclaredMethod("addURL", parameters);

            method.setAccessible(true);
            method.invoke(sysLoader, new Object[] { u });

        } catch (Throwable t) {
            t.printStackTrace(System.err);
            throw new IOException("Error, could not add file " + u.toExternalForm() + " to system classloader");
        }

        return sysLoader;
}

我现在需要的不是将 .jar 文件放在本地路径中,而是直接像我们通过调用 .jnlp 的按钮进入浏览器时执行的操作一样。

基本上是调用例如xpto/test.jnlp。将 File file = new File("D:\test\mytest.jar") 替换为 URLfile = new URL("xpto/test.jnlp")。

有可能的?如何调整上面的代码以通过 jnlp 执行并使用其余代码?

非常感谢提前问候

4

0 回答 0