我在使用类加载器时遇到问题。我的代码片段:
[...]
Class<?> appletClass = classLoader.loadClass("path.to.Applet123");
Applet applet = (Applet) appletClass.newInstance();
applet.init();
applet.start();
[...]
Applet123 类不是我的,所以我无法编辑它。但我知道,在 Applet123 类中是这样的:
public void init() {
System.out.println(getParameter("myParameter"));
}
不幸的是,它打印出 null
.
我需要在代码中添加什么来使用参数 myParameter
加载 Applet123.class包含字符串,例如“你好”?
感谢您的回复。
最佳答案
如果您确实需要自己加载小程序,您还需要提供一个 AppletStub 实例,Applet 实例从中读取参数。 Java 源代码显示了这一点:
public String getParameter(String name) {
return stub.getParameter(name);
}
请注意,许多方法从 stub 实例获取数据,因此您可以执行以下操作并填补空白,或者(可能更好)使用 @owlstead 提到的 JNLP!
AppletStub stub = new AppletStub() {
// lots of code including defining the parameter 'myParameter'
};
Applet a = new Applet();
a.setStub(stub);
a.init();
// ...
关于java - ClassLoader 加载带参数的小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265428/