cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

JavaWebStartSupportInNetBeansIDE

Java Web Start support in NetBeans IDE

In this article you will learn how to deploy an existing desktop application using Java Web Start technology using NetBeans IDE.

Here is the screenshot of nice desktop database application running locally on your computer. What about to deploy it to server and launch it from web browser via Java Web Start?

Image:application_JavaWebStartSupportInNetBeansIDE.png

We can do it easily in NetBeans IDE!

Making the project Web Start enabled

Provided that the project is already running as desktop application the first step is really easy - just make the project Java Web Start enabled:

  • Open Project properties of the project and select node Webstart
  • Click the checkbox 'Enable Web Start' and also 'Self-signed', because the application needs to be signed to be able to access all resources
  • You can also click 'Allow Offline' if you want the application to run without internet connection (of course after the application is once downloaded)
  • Close project properties dialog - new run configuration called WebStart for running the project via Java Web Start is created

Checking 'Self-signed' checkbox means that all jar files will be signed by self-signed certificate and application will be able to access the same resources from the computer as regular application running locally. Self-signed certificate is created during the build of the project.

There are also other web start related properties that are on panel called Application in Project Properties dialog, those properties are application title, vendor, description, homepage and splash screen. Values of those properties are usually shown to user either during downloading or starting the application.

Now it's possible to run the project via Web Start. Check whether the project is set as main, if not, invoke context menu and select 'Set as Main Project'. Then check also that selected project configuration on Main Window is 'WebStart', if not, select it. Now you can hit F6 and project will be executed via Java Web Start. When application runs via Web Start the splash screen 'Java Starting ...' is shown and then you will be asked whether signed application can be executed. If you hit Run the application will be started.

Image:security_question_JavaWebStartSupportInNetBeansIDE.png

Java Web Start related files

Now let's observe what Java Web Start related files were actually created. Switch to Files tab in Explorer, expand the project node and also expand dist folder. You can see that there are two more files comparing to regular Java application project. File launch.jnlp is the Java Web Start descriptor needed to run the application.

Here is example of the JNLP file for our application:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="file:/home/milan/Development/jnlp_article/projects/RealityDesktopApplication/dist/" href="launch.jnlp">
    <information>
        <title>Database Application Example</title>
        <vendor>NetBeans</vendor>
        <homepage href="http://appframework.dev.java.net"/>
        <description>An empty application shell to be used as a basis for Swing applications working with databases</description>
        <description kind="short">Database Application Example</description>
        <offline-allowed/>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.5+"/&gt
        <jar href="RealityDesktopApplication.jar" main="true" download="eager"/>
        <jar href="lib/RealityLibrary.jar" download="eager"/>
        <jar href="lib/appframework-1.0.3.jar" download="eager"/>
        <jar href="lib/beansbinding-1.2.1.jar" download="eager"/>
        <jar href="lib/db.jar" download="eager"/>
        <jar href="lib/derby.jar" download="eager"/>
        <jar href="lib/swing-worker-1.1.jar" download="eager"/>
        <jar href="lib/swingx-0.9.2.jar" download="eager"/>
        <jar href="lib/swingx-ws.jar" download="eager"/>
        <jar href="lib/toplink-essentials-agent.jar" download="eager"/>
        <jar href="lib/toplink-essentials.jar" download="eager"/>
    </resources>
    <application-desc main-class="realitydesktopapplication.RealityDesktopApplication">
    </application-desc>
</jnlp>

You can see that all important information about the application are here, all the Jar files under resources element are actually application classpath set in Project Properties. Please note that the attribute codebase of jnlp element points to local folder, it means that the application can run only locally. We will fix it later.

Along with jnlp descriptor there is also launch.html file, that can be used to run the application from web browser. There is a section in this file that is commented out and this section if uncommented takes advantage of Java Deployment Toolkit which helps to localize right JRE on target computer (more about the Deployment Toolkit).

This is code snippet for utilizing Deployment Toolkit to launch Java Web Start application:

<script src="http://java.com/js/deployJava.js"></script>
<script>
    var url="http://[FillInYourURL]/launch.jnlp";
    deployJava.createWebStartLaunchButton(url, "1.6");
</script>

Database connection

When creating such desktop database application you need to have live database connection, this application uses Java DB database (part of JDK 1.6) in embedded mode. The application will be deployed via Java Web Start so we need to change the database location from local folder to jar file that will be bundled with the application. Such database will be read only. Our application uses Java Persistence API, so we will modify persistence.xml file:

  • Switch to Projects view and open META-INF/persistence.xml file
  • Change toplink.jdbc.url property value from "jdbc:derby:/some/folder/RealityDB" to "jdbc:derby:classpath:RealityDB", this URL means that database is accessible through classpath, usually stored in Jar file
  • Add the Jar file with database to project classpath

Jar file with the application is just simple jar file of the database folder without any special handling.

Now the application is basically ready to be deployed on some web server, you can run it locally to test it and once you know where the application will be deployed you need to fix the codebase to be the real URL on the server. Setting up the codebase:

  • Open Project Properties of the project and select node Webstart
  • Change Codebase to User defined and enter real codebase URL into Codebase Preview text field

After rebuild the project is ready for deployment. Application must be deployed by publishing whole content of dist folder as is, if there are any changes in directory structure they must be reflected in launch.jnlp file.

When all required files are published you are ready to Launch the Application

References: