Reputation: 43
First time posting here. I made a simple calculator program using java and I am trying to put it onto my website. From what I've gathered from previous help posts is that I need to create a JApplet with all my program contents and compress it into a .jar file. Then I need to create a .JNLP file, which describes how to applet should be launched.
So here is where I am having trouble.
package calculator;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
public class CalculatorApplet extends JApplet {
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run() {
Calculator calc = new Calculator();
add(calc);
}
});
}
catch(Exception e)
{
System.err.println("GUI creation failed");
}
}
}
It seems my applet was not constructed properly. Whenever I run it a "java.lang.reflect.InvocationTargetException" is thrown. Whenever I run my Calculator class independently from the applet it works as expected. Any ideas where the source of my error is?
Upvotes: 4
Views: 713
Reputation: 31
You can certainly use JNLP with applets. See https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/runAppletFunction.html and https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/embeddingJNLPFileInWebPage.html
Upvotes: 0
Reputation: 2381
I think JNLP files are used for Java Web Start. That's something you won't need with an average Java applet. Please do correct me if I'm wrong.
If you have the working .jar file, an HTML file that calls the applet will be sufficient to run the applet. Insert the code <applet width="300" height="300" archive="jar.jar" code="class.class"></applet>
into an HTML file, where class.class
is the class extending Applet or JApplet and jar.jar
the location of the jar file. Loading the HTML file in a browser will display the applet.
Alternatively, you can use Java's Applet Viewer to open the HTML page and open the applet locally.
Upvotes: 1