Guest blog by David Farkas Microsoft Student Partner at the University of Cambridge
I have started immersing myself in the creative side of technology just a few years ago. I’ll be returning to Cambridge in the fall, to further my education in Computer Science.
My LinkedIn: https://www.linkedin.com/in/david-farkas-3b00a8a1/
My git: https://github.com/Veraghin
Nowadays, the Cloud is increasingly part of the life of all programmers, as most applications are dealing with or running on the internet. Microsoft Azure is one of these Cloud platforms. I’ve spent my first year in Cambridge mostly programming in Java, thus it was a given that I would try and see how Java and Azure could be used together.
The starting point is https://azure.microsoft.com/en-us/develop/java/ , which has a collection of links to detailed tutorials about using Java on Azure. After a bit of research, I decided to build on the guide about creating a basic Azure Web app, using Eclipse.
This tutorial, which can be found at https://docs.microsoft.com/en-us/azure/app-service-web/app-service-web-eclipse-create-hello-world-web-app, gave the backbone to my project.
In this post, I’ll be going over the basics of how to convert an existing Java desktop application to an Applet and my experience on hosting it on Azure.
I’m assuming you have Eclipse set up already on your computer. You will need the Azure Toolkit for Eclipse installed, which can be found in the Eclipse marketplace. By doing this, you can deploy and manage applications running on azure from the Eclipse IDE itself.
You’ll also need to make sure you have the Eclipse Java EE Developer Tools installed, also from the Eclipse marketplace, it helps you create web applications in Eclipse.
My code can be downloaded from https://github.com/Veraghin/GameOfLifeApplet
The process of converting from a desktop application to an Applet
The original project was the implementation of Conway’s Game of Life in Java. Converting an existing Java desktop application which utilizes Java Swing for the GUI is relatively straightforward, the top class, extending JFrame can be changed to extend JPanel, and after removing some functions from the constructor which are not needed for an applet, the JPanel can be included as the content pane of a new skeleton applet class.
In this case, my original top class was GUILife, extending the JFrame swing class, which had to change to JPanel, as the applet will be embedded in a website and it can’t have its own window. Also, the constructor had to change from:
The super(“Game of Life”) call, which would set the window name of the JFrame is redundant, the html will take care of it along with setting the applet size. The setDefaultCloseOperation is uncalled for, it will be handled by the browser running the application.
The following code is the skeleton applet, into which the former desktop application is inserted as its content panel:
This could be run as an applet on its own, but I set out to embed it in a website. To achieve that, I’ll use the Dynamic Web project in Eclipse, following the tutorial linked at the top.
The Eclipse provided Dynamic Web project template automatically creates an index.jsp file, which is our homepage on the website, this is where the applet is
To set things up this far, create a Dynamic Web Project in Eclipse, then copy the source code from GitHub to the new Web Content folder.
From here, the project can be deployed to Azure straight away. Left clicking on the project name and selecting Azure -> Publish as Azure Web App… takes you to the Azure login screen, and then you can create or select the App service you want to deploy to. The whole process is really straightforward and well documented, it makes deploying straight to Azure an easy process.
When creating a new App service, the Azure portal provides more information, but you can customize all the important parts, such as location, pricing tier, Java and Web container versions straight from Eclipse.
The Final Implementation
This is an unsigned application, so getting it to run requires jumping through a few hoops, but it is all done for the sake of security. Starting with Java 7 Update 51, applets that are not signed by a trusted authority are not allowed to run in the browser, but this can be circumvented by adding the applet’s URL to the exception list in the Java Control Panel, which can be done by following this guide to adding an URL to the Exception list: https://java.com/en/download/faq/exception_sitelist.xml
More info on the security impact of applets:
The website itself can be found under the following URL, it works under Internet Explorer:
For testing purposes, to see the applet running you can call “appletviewer index.jsp” in the command line in a folder containing the source files. The appletviewer command is part of the Java SDK.
Some pictures of the final version: