Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 246 posts at DZone. You can read more from them at their website. View Full User Profile

Running a WAR as a Desktop Application

05.03.2010
| 9703 views |
  • submit to reddit

If you have written a web application, the next logical step is to make it available offline. The long-term solution is clear: You give your web application an offline mode, which will hopefully be complemented by explicit application management in web browsers. Short- to mid-term, though, that is often not feasible, because the server provides crucial functionality to the client.

Thus, I was looking for a different solution. One option I had seen was Hudson’s self-executable WAR file: When you execute java -jar hudson.war, an embedded web server starts and you can immediately try out Hudson. This approach had two limitations that I didn’t like. First, I wanted my solution to be acceptable for end users, so I wanted a graphical user interface. Second, the embedded web server extracted the WAR file to a temporary directory and did so again for each startup, wasting time. In contrast, my solution does the following things:

  • The application is a JAR file, the WAR is embedded inside. I opted against a single binary for JAR and WAR, because packaging the JAR as a nice desktop application adds platform-specific data, anyway, and makes it often impossible to deploy the result as a WAR.
  • When the JAR starts up, a Swing user interface is shown. If this hasn’t been done before, the WAR file is extracted to a directory next to the JAR file. The idea is that there is a dedicated folder for the application in which the JAR resides. The location of the JAR is determined via this trick. The WAR file (=ZIP format) in the classpath is extracted to the file system using the standard Java API.
  • Next, a web server is started and pointed to the web application directory in the file system (none of the web servers I’ve seen is able to serve a WAR file directly, let alone one that is embedded inside a JAR file). I used Winstone, because it is so small. There are even smaller ones, but those won’t allow you to use servlets. Obviously, any embeddable Java servlet container will do.
  • A button allows the user to open the starting page of the web application in the default web browser. java.awt.Desktop (Java 6) allows you to do this. Currently, localhost and a fixed port is used. In the future, the port should be configurable and one could maybe automatically switch to a different port if the default one is occupied.
  • Finally, I also produced a Mac OS X application. Such an application is a folder with the file name extension “.app” and the JAR file inside. This has the nice side effect of hiding the extracted WAR directory.
This is all there is to it. Now even non-technical end users can try out my web application without having to go through the steps of downloading and installing a web server. You can download the Eclipse project of my implementation (be warned, the code is quite experimental).

 

From http://2ality.blogspot.com/2010/05/running-war-as-desktop-application.html

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Gervais Blaise replied on Tue, 2010/05/04 - 2:03am

Yeah cool. That can be really cool for web apps demo. But what's about the database ?

Axel Rauschmayer replied on Tue, 2010/05/04 - 4:40am in response to: Gervais Blaise

I find that having an external database is always a bit of a nuisance (when it comes to ease of installation). So I would always go for an embedded one. Then the problem is: Where do you put your files? This has been discussed here, with great ideas in the comments.

Dmitry Leskov replied on Tue, 2010/05/04 - 5:24am

Why not go one step further and get rid of the JRE dependency too?

See e.g. Pebble blog engine packaged as Windows and Linux installers.

Alexey Solofnenko replied on Tue, 2010/05/04 - 6:09pm

Extracting a war file seems an unnecessary step. All classes can be already in the current jar and files can be accesseng via JAR URL.

Axel Rauschmayer replied on Tue, 2010/05/04 - 6:15pm in response to: Alexey Solofnenko

I agree. One could even put the (unpacked) contents of the WAR inside the JAR. I just don't know of any servlet container that is able to serve a WAR without unpacking it.

Loren Kratzke replied on Tue, 2010/05/04 - 11:50pm

I give full credit for creative thinking. If nothing else you have successfully traversed several dark corners of the platform in pursuing this project. At best you may be on to someting pretty cool indeed. The concept of an embedded webserver seems to be catching on lately. And embedded databases are very cool too.

My DB solution is to use Derby and just create (the damn) database rooted off home somehwere unless a local property tells me otherwise. Works every time. One can also opt to create one within the context root or work directory, but it may get toasted upon subsequent deploys - good for demo purposes anyway.

Mark Unknown replied on Thu, 2011/02/03 - 9:53am

There is no date on this article (see link below) but i am sure it is at least 5 years ago. I used it to build a something that sales people could demo from their laptops by just plugging in a USB drive.

I have also done variations of this. One of them is running an Eclipse RCP that starts and H2 db and then Jetty, then opens the app.

http://www.javalobby.org/articles/tomcat2go/

Sham Bhise replied on Sun, 2014/07/13 - 5:21am

i have developed one application which web and i want to install that application on a desktop(i.e my client).The client doesnt want to go online and my problem is that i want to keep my files safe to avoid modification or damage to that application.Is there any solution to keep the files safe in client desktop or laptop..please help me

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.