简体   繁体   中英

How to: JAR -> Web hosted application?

I've been looking around so please excuse me if I missed it, I know there are a lot of threads that deal with this sort of thing, but I haven't found a clear answer yet.

My question is (I think) quite simple. I have an executable JAR file (created in Eclipse) and I want to host it on my website, presumably as an Applet. I have little understanding of applets, just that they are Java executables that run on webpages.

Is this transition trivial, or does will it require me to pretty rewrite my program entirely? Baring in mind it relies heavily on Swing components such as JTabbedPanes, JButtons, JLabels, etc.

Thanks!

EDIT: Thanks to an answer, I've rewritten my components to be applet friendly inside of a class which extends Applet and build my GUI as you did within the run() method. My problem now is that before, I had a run method for the program itself (it's a simulation of sorts). Within, it simply updated certain things over time, but did so in an infinite loop. Without this run code, my applet can build the GUI, but the GUI stands no purpose without the run code, which causes the applet to fail to load any components. Any thoughts?

EDIT_2: I've just decided to use Java Web Start. Thanks.

You don't need to convert it into an applet. You can just use Java Web Start.

You need to declare an applet-extending class, which will be the entry point for your applet:

public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

From the run method (in the example) or the init() , you can perform everything that your program does. Next, you need to specify your applet in the browser html:

<applet code="yourMainClass.class" height="250" width="350">
  <b>Sorry, you need Java to play this game.</b>
</applet>

To conclude: you need to rewrite your entry point class (the one containing main method), from where you need to call all the same Swing-building functions. And repackage everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM