How can I change my application into applet?

John Zukowski

It really depends on the type of application you have and if you still need to run the converted applet as an application.

If the application is not graphical, then your new applet would just be a graphical frontend to execute it. Depending upon what the application needed to do, that might involve actually executing something on the server, as applets have security restrictions. You would need to develop the communication infrastructure to support this. For instance, a chat application run as an applet would require some kind of a proxy-like server on the web server to redirect the chats between different users running applets.

If on the other hand your application was graphical in nature, you would have to change it to display in a subclass of java.applet.Applet, instead of your main window. If you still wanted to have your main window be a Frame, then your applet would need to create and open it based upon some user interaction with the applet. As long as your Frame-based application didn't try to do anything it couldn't from the applet security model, this is actually the easiest approach.

If the application needs support files, like images or audio files, how to get these tends to be different between applets and applications. It is best to get these as resource files [getResource()/getResourceAsStream()] from both applications and applets then try to do things differently between the two. And, keep in mind that applets can only get them from the web server from which the applet came, not a third-party server which the application would be able to communciate with.

So, a rough set of steps to do the conversion would be as follows:

  • Create a subclass of java.applet.Applet
  • Take the code in the main() method of the application and move it to the init() method of the applet
  • Create an HTML file to load the applet
  • If necessary, move any requirements that untrusted applets cannot do into an appropriate server-side task
  • If necessary, modify all getting of support files
  • Test