Thursday 7 June 2012

Bash script to extract dependencies from Java WebStart

A quick script to retrieve all the WebStart artifacts from a JNLP file and generate a corresponding classpath:


wget -Nrq -nd $SERVER/html/jaws/calypso.jnlp
for jar in `cat calypso.jnlp | grep jar | cut -d"\"" -f 2`
        CP="$CP:./lib/"`echo $jar | cut -d"/" -f 3`
        wget -Nrq -nd $SERVER$jar
echo $CP > classpath

The function of the script is pretty simple, it retrieves the JNLP file from the server and then extracts all jar references in the file to retrieve the corresponding JAR files. As the JAR files are being retrieved they are added (in the correct order) to a variable to be used as the classpath. 

The need for this script arose from a requirement to have a client application launch using the correct dependencies and classpath ordering as used by the WebStart application. An alternative approach would have been to create a new WebStart JNLP with the new application as the entry point, unfortunately this was not practical given the organisational constraints (no access to deploy the change and 6+ week lead time for deployment via the approved process).

Some useful side effects of this approach:
  • It can be used to launch the application in the JNLP without requiring a WebStart launcher
  • The application can be launched with additional JVM arguments not supported by WebStart
A few notes on the script (as always):
  1. The -Nrq command line option to wget uses time stamps (-N) to ensure only new files are downloaded. The existing files are overwritten (-r) with the latest version, rather than having a number appended to the file name as is the default behaviour for wget. The output from wget is suppressed by the -q option so as not to print out unnecessary information.
  2. The -nd command line option prevents wget from creating directory structures when retrieving the files, this simplifies the classpath creation.
  3. The generation of the classpath could be improved with a regular expression rather than expecting a specific format in the JNLP file.