1

I have a problem with using ClassLoader. Snippet of my code:

[...]

Class<?> appletClass = classLoader.loadClass("path.to.Applet123");
Applet applet = (Applet) appletClass.newInstance();
applet.init();
applet.start();

[...]

And Applet123 class isn't mine, so I can't edit it. But I know, that in the class Applet123 is something like this:

public void init() {
    System.out.println(getParameter("myParameter"));
}

Unfortunately it prints null.

What have I to add to my code to load Applet123.class with parameter myParameter containing String e.g. "Hello"?

Thanks for replies.

vrbadev
  • 455
  • 1
  • 7
  • 20
  • *"I have a problem with using ClassLoader."* Why are *you* trying to load the applet as opposed to embedding it in a web page or launching it free-floating using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Jun 25 '13 at 01:19

2 Answers2

1

If you really need to load applets yourself you will also need to provide an AppletStub instance from which the Applet instance reads the parameters from. The Java source code shows this:

public String getParameter(String name) {
    return stub.getParameter(name);
}

Note that many methods get data from the stub instance, so you could do the following and fill the gaps OR (likely better) use the JNLP as @owlstead mentioned!

AppletStub stub = new AppletStub() {
    // lots of code including defining the parameter 'myParameter'
};
Applet a = new Applet();
a.setStub(stub);
a.init();
// ...
clearwater
  • 514
  • 2
  • 7
0

Take a look at the tutorial by Oracle:

http://docs.oracle.com/javase/tutorial/deployment/applet/param.html

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <!-- ... -->
    <applet-desc
         name="Applet Takes Params"
         main-class="AppletTakesParams"
         width="800"
         height="50">
             <param name="paramStr"
                 value="someString"/>
             <param name="paramInt" value="22"/>
     </applet-desc>
     <!-- ... -->
</jnlp>
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263