Distribute your Swing application via Java Web Start!

You have developed a nice desktop application and have been breaking your head to create an executable to distribute for different platforms! Here comes Java Web Start for your rescue, distribute your Swing application via web, no installers, no custom code for desktop shortcuts, no tweaks for auto-upgrades! Let me explain the process step by step.

Step 1: Jar your classes and resources using your favorite build tool.
Say your jar file is myswingapp.jar, rename it to myswingapp__V1.0.jar, ie, add '__V' and an initial version say 1.0. I will explain why this is needed in the coming section.
Step 2: Organize your dependencies as below:
root _folder>myswingapp__V1.0.jar
root _folder>jarfile1.jar
root _folder>jarfile2.jar
root _folder>jarfile3.jar
Step 3: Create a jnlp file as below and save it as myswingapp.jnlp

<?xml version="1.0" encoding="UTF-8"?>
<jnlp codebase="http://mysite.com/downloads/" 
        href="myswingapp.jnlp">
    <information>
        <title>MySwingApp</title>
        <vendor>MyOrg</vendor>
        <description>A swing application</description>
        <icon href='logo48.png' kind='default'/>
        <icon kind="shortcut" href="icon.jpg" width="32" height="32"/>
        <offline-allowed/> 
        <shortcut online="false">
       <desktop/>
       <menu submenu="MySwingApp"/>
     </shortcut>
    </information>
    <security>
       <all-permissions/>
 </security>
 <update check="background"/>
    <resources>
     <j2se version="1.6.0+"/>
        <jar href="myswingapp.jar" main="true" version="1.0"/>
        <jar href="jarfile1.jar"/>
        <jar href="jarfile2.jar "/>
        <jar href="jarfile3.jar "/>
        <property name="jnlp.versionEnabled" value="true"/>
    </resources>
    <application-desc main-class="com.myorg.Main"/>
</jnlp>

Step 4: Copy all your jar files and the jnlp file to  mysite.com/downloads/
Step 5: Create your download page say download.html and add the below script:

<script src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript"> 
    var url = "http://mysite.com/downloads/myswingapp.jnlp";
    deployJava.createWebStartLaunchButton(url, '1.6.0');
</script>

Step 6: You are done! Now you can launch your application by accessing your download page, just click on the 'Launch' button, the installer will download your application and install it with a desktop shortcut as well as a menu item to launch the application. Great, isn't it?

Signing jars: If you want your application to run with full access, you must sign all your jar files.
Updates: An update check will be performed in the background and if an update is available, the application will download it and install it in the background! You just need to put your latest jar files, that's it!

Feel free to drop your comments. I will address your queries based on time availability!

Create Your Own Programming Language

A system to achieve every programmer’s dream. Learn how to create a simple programming language in a few days with this easy step-by-step guide.

6 Comments

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.

  1. hi,
    I have the file myswingapp__V1.0 and myswingapp.jnlp as you gave as an example in my directory. But when I click it gives the error;
    unable to load resource: (file://localhost/C:/documents and settings/user/desktop/myswingapp.jar?version-id=1.0, 1.0)
    at com.sun.deploy.net.DownloadEngine.actionDownload(Unknown Source)
    ..
    ..
    ..

    Also it gives a Wrapped Exception Error:
    java.io.FileNotFoundException: file://localhost/C:/documents and settings/user/desktop/myswingapp.jar (System could not find the file)
    at java.io.FileInputStream.open(Native Method)
    ...
    ..
    ..

    When I erase these parts from jnlp file and make my jar file as myswingapp.jar it works;
    update check="background"/

    property name="jnlp.versionEnabled" value="true"/

    and

    version="1.0" part at jar href="myswingapp.jar" main="true" ...

    Can you help me with this problem?

    ReplyDelete
  2. Hi Barbaros,

    You should follow the naming convention jarfile__V1.0.jar

    Cheers,
    Sunil.

    ReplyDelete
  3. I mentioned wrong in the first post but I have already named my file as myswingapp__V1.0.jar and have those problems.

    ReplyDelete
  4. Deploy your jnlp file in a web server, change your base url in jnlp accordingly.

    Also recommend you to upgrade to the latest version of Java as Java Web Start is not stable in any of the previous releases.

    Let me know whether it works or not.

    ReplyDelete
  5. how the jnlp file will executed

    ReplyDelete
    Replies
    1. Command line: javaw
      Some browsers automatically opens jnlp file on clicking jnlp file link.

      Delete
Post a Comment
Previous Post Next Post