Automatic JNLP Generator

Java Web Start is a very cool tool for distributing Java programs online. The end user clicks a link, and the Jar file is downloaded and launched without the user having to do anything, other than have Java installed and, if the program needs extra access like file permissions, confirm that they trust the download.

However despite its ease for the end user, it's kind of a pain to set up, and there's no great documentation on doing so. One of the biggest hassles with JWS is configuring the JNLP files, as they require a full path (including protocol and domain name) to specify the codebase where the Jar(s) are located. This can get pretty irritating when testing and deploying.

So I developed a PHP class which takes a few simple parameters and generates the codebase on the fly. It's a limited tool intended for simple JWS deployments (if you're doing something complicated enough that this tool cannot work, you'll probably want to be custom creating your JNLP files anyways) but should be more than powerful enough to get a program you've been developing online. And of course you can always extend this tool if need be.

Using This Tool

Once your Jar is good to go, you can put it on your webserver along with jnlp.class.php. Then create a new PHP file in the same directory as the Jar and, after including jnlp.class.php enter something like the following:

$jnlp = new jnlp(); $jnlp->setInfo('PROGRAM_TITLE','PROGRAM_DEVELOPER', 'HOMEPAGE', 'PROGRAM_DESCRIPTION'); $jnlp->addJar('RELATIVE_PATH_TO_JAR',true); $jnlp->setMain('PACKAGE_AND_CLASSNAME_OF_MAIN_METHOD'); $jnlp->getJNLP();

This configures the JNLP file and, when you load this page, will serve it up as a JNLP XML file, dynamically specifying the hostname and path to the Jar so you don't have to, and allowing the same exact file to work on your development server and your live server. Further documentation can be found in the source code.


Download: PHP Source File
Size: 5.55kb

Create A JNLP Right Now

If you don't want to download the code, or don't have access to a PHP server (really? switch hosts.) you can build a JNLP file right now online using this tool. It's slightly more limited than the class (limited to 3 Jars, for instance) but should work just fine to quickly pull together simple JNLPs.

Signing Your Jars

One thing to be aware of, is with JWS any programs intending to do anything on or with the user's computer (like local file access, for instance) need to have signed Jars. If you don't know how to sign a Jar, it's actually surprisingly simple, but it does involve a (very) little bit of command prompt-fu. If you've been using an IDE like Eclipse and running Windows, you've probably never configured Java to work on the command line. If you need to, follow step 4 of Oracle's instructions on configuring Java for the Windows command prompt. Once it's configured (you can test this by running javac -version) you can go ahead and sign the Jar file.

In the command prompt, navigate to the directory your Jar file is located in and enter the following:

keytool -genkey -alias KEYNAME

Substituting KEYNAME for a name to call your new key. This command creates a private key for you to sign your Jars with. It will prompt you for some information, fill out what you think is necessary. You can use the -keystore flag if you don't want the keys saved in the default location, but really it's unlikely you'll need to do that unless you're working on a large distributed project.

Now just sign the jar with this command:

jarsigner JNLPTime.jar KEYNAME

Use the same KEYNAME as before, and if you used a special keystore, specify that again as well. Now your Jar is signed and you can access more features on your client's computer.

How can you tell if you need to sign your Jar? If you need to call setSecurity() when building your JNLP file, you will need to sign your Jar. No security permissions (the default) does not require signed Jars. Anything else does.

More information on generating keys and signing Jars.