Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java tip: Deploying JavaFX apps to multiple environments

Get around the 'gotchas' of JavaFX deployment

  • Print
  • Feedback

JavaFX 2.0.2 and successor SDKs let you deploy JavaFX applications in multiple environments including as a standalone app, via Java Web Start, or embedded in a web page. Jeff Friesen follows up his recent introduction to JavaFX 2.0 with this short tip on deploying JavaFX applications.

If you've read my three-part introduction to JavaFX 2.0, then you will be familiar with JPadFX, the Swing-based notepad that I used as a refactoring example for that series. Even if you're not familiar with JPadFX, you should be able to follow this tip. We'll concentrate on deploying the application, so you won't need to know much about its internals. Do go ahead and download JPadFX now.

Prepare JPadFX for deployment

Source files for the JPadFX app are JPad.java, About.java, Alert.java, AreYouSure.java, and the resource file icon.png. Once you have extracted these files to your current directory, execute the following command to compile the app on Windows:

javac -cp "C:\Program Files\Oracle\JavaFX 2.0
SDK\rt\lib\jfxrt.jar";. -d out JPadFX.java

I assume that you have installed the JavaFX 2.0.2 SDK to C:\Program Files\Oracle\JavaFX 2.0 SDK. Also, -d out stores the resulting class files in a subdirectory named out.

Deploying JPadFX as a standalone JavaFX application

JavaFX 2.0.2 provides a command-line tool, javafxpackager, that makes application deployment almost a snap. On my Windows XP platform, javafxpackager is installed in the C:\Program Files\Oracle\JavaFX 2.0 SDK\bin directory.

Once you have compiled JPadFX's source files, use javafxpackager to package the class files and JavaFX Launcher (a small set of classes that finds the installed JavaFX runtime and invokes the main application class via the runtime) into a JAR file as follows:

Listing 1. Package JPadFX with javafxpackager

javafxpackager -createjar -appclass JPadFX -srcdir out -outdir out -outfile
jpadfx.jar -v

Note the options specified in Listing 1:

  • -createjar tells javafxpackager to create a JAR file.
  • -appclass identifies JPadFX as the main class for the JAR manifest and JavaFX Launcher.
  • -srcdir identifies out as the directory containing the class files.
  • -outdir identifies out as the directory in which to create the JAR file.
  • -outfile identifies jpadfx.jar as the name of the JAR file.
  • -v specifies verbose output, which can be useful should an error message be displayed.

If the previous command succeeds, you will need to introduce icon.png to jpadfx.jar. A simple way to do this is to use the JDK's jar tool. Assuming that the current directory contains icon.png and the out subdirectory, execute the following command to store icon.png in jpadfx.jar:

jar uf
out\jpadfx.jar icon.png

At this point, you might want to try running JPadFX in standalone mode in order to test the JAR file's veracity. First, switch to the out directory (which contains jpadfx.jar) and execute a command similar to the following:

java -cp "C:\Program Files\Oracle\JavaFX 2.0
SDK\rt\lib\jfxrt.jar";. -jar jpadfx.jar

This command adds jfxrt.jar to java's classpath, which contains JavaFX class files that the JVM needs to load at runtime.

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.