Adding an applet to the site

First post here. I made a simple calculator program using java and I am trying to put it on my site. From what I collected from previous posts, I have to create a JApplet with all the contents of my program and compress it into a .jar file. Then I need to create a .JNLP file that describes how the applet should be launched.

So, this is where I have problems.

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 built properly. Whenever I run it, a java.lang.reflect.InvocationTargetException is thrown. Whenever I run my Calculator class regardless of the applet, it works as expected. Any ideas where the source of my error is?

+4
source share
2 answers

I think JNLP files are used for Java Web Start. This is something you will not need with a middle Java applet. Please correct me if I am wrong.

If you have a working .jar file, the HTML file that calls the applet is enough to run the applet. Paste the code <applet width="300" height="300" archive="jar.jar" code="class.class"></applet> into the HTML file, where class.class is the class extending the applet or JApplet and jar.jar location of the jar file. Downloading the HTML file in the browser will display the applet.

Alternatively, you can use the Java Applet Viewer to open the HTML page and open the applet locally.

+1
source

Source: https://habr.com/ru/post/1446177/


All Articles