A few days ago I was playing with JAX-RS and Jersey when I thought “oh, what easy it is to run this kind of web applications” because of you don’t need a servlet container. Wait! I can run this applications using a plain main() method so… these kinds of application are desktop applications? I can make a main() method that launches the Http server and opens the web application in the default web browser. Mmmm and I can put a tray-icon to re-open web browser if the user closes it or to stop the web server. On balance your main() method does these things:
- Launches the Http server that comes with Java SE 6
- Opens the default web browser to any URL inside your application (http://localhost:8081/something)
- Places a tray-icon that can be used to open the main URL (http://localhost:8081/something) or to stop the Http server.
How? With Java SE 6 is easy with its new Desktop and System Tray APIs. You can see sample source code of all these ideas.
When you are done you can distribute your application in a JAR executable file or you can wrap it into an exe file. Do you need a database? You can use Java DB included in Java SE 6 or other embedded database.
So… what are we doing? We are distributing a web application as a desktop application. What is this useful for?
- Prototyping and demostrating: you can send your application to someone without more requirement that a standard Java SE 6 runtime installation.
- Web applications working on-line and off-line. You can create two versions of your application sharing most of the code. One version will be a “desktop application” and the other one will run in a servlet container. If the user is off-line he can use the desktop version, and when he goes on-line the application can populate the changes to the server. So the desktop version can act as a data cache. The user can also work on-line in any workstation with the on-line version wich has the same user interface that the other one.
- You want to create a desktop application and you love web design and development.
- You want to create a mash-up desktop application using services such as Google Maps that are more easy to use with an HTML + CSS + JavaScript interface.