Packaging a JavaFX Project as a Stand-Alone jar File
These instructions illustrate how use javafxpackager to create a stand-alone jar file of a JavaFX application, as required in Stage 5 of your assignment. The javafxpackager command is docummented on the Oracle web site here.
Please note that you may find that you can create the jar more easily using eclipse's export function. To do this, selected "File", "Export...", "Runnable Jar File" and ensure that you select "Package required libraries into generated JAR". Be sure to test the generated jar file on the lab machines!
Note the example below works in the lab. If you're running MacOS or Windows, you'll find that javafxpackager is in a different location (for example, it is in /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/bin/javafxpackager on my laptop). So, if you want to package the jar on your laptop you'll need to find where javafxpackager is on your computer (use the file finder on your OS).
In the following example, I show how to build a jar file for the game developed in lectures 11 and 12. It would be wise to confirm that you can run the game correctly directly from eclipse before attempting to do so via a jar file. To do this, ensure that your lecture code is up to date (pull and update), and that you have your build paths set correctly including your JavaFX path. Once you have done that, select CaterpillarGame from within lecture 11, and run it (using the green run button or the menu). You should see the game window open and you should be able to play the simple game.
Once you have successfully confirmed that the game works from within Eclipse (as per above), you can create a jar file as follows:
- Open a terminal.
- Identify the location of the directory that contains your class files for the caterpillar game. We will call that directory srcdir in this example.
- You can do this with the unix find command, as follows: find . -name CaterpillarGame.class. In my example, when I run the command on the lab machine, it looks like this:
find . -name CaterpillarGame.class ./workspace/COMP1110 lectures/bin/_11_game/CaterpillarGame.class
The part of this ending with '/bin' (i.e. ./workspace/COMP1110 lectures/bin) identifies srcdir, the location of the folder containing the classes. Your srcdir may be different depending on where your workspace is and the name you gave to the project containing the lecture slides. - If you are unable to find the class in the step above, this suggests that you have not got this working in eclipse, as per the preamble above.
- You can do this with the unix find command, as follows: find . -name CaterpillarGame.class. In my example, when I run the command on the lab machine, it looks like this:
- Once you have established srcdir (./workspace/COMP1110 lectures/bin), and the name of your package and class, separated with a dot (_11_game.CaterpillarGame), you are ready to create a jar. (If your class were in the default package, you should just write the class name, with no dot.)
- You should execute the command:
/usr/local/javafx/bin/javafxpackager -createjar -appclass <package>.<class> -srcdir <srcdir> -outdir . -outfile game -v
which in this example becomes:/usr/local/javafx/bin/javafxpackager -createjar -appclass _11_game.CaterpillarGame -srcdir ./workspace/COMP1110\ lectures/bin -outdir . -outfile game -v
(Notice that the space in my srcdir must be written as '\ ', not ' '). - If the command executed correctly, you should now have a jar file, game.jar.
- You should execute the command:
- You should now be able to execute the game by typing
java -jar game.jar
- Unfortunately because there are still some wrinkles in the still-new JavaFX, the above won't quite work on computers where JavaFX is installed separately to the runtime. In that case you'll run into a window that says "This application requires a newer version of the Java runtime. Please download and install the latest Java runtime from java.com. Then restart the application." On a Mac or Windows you probably won't see that problem because you probably installed JavaFX as part of Java.
- To address the problem (on the lab machines), you need to explicitly tell Java where to find java FX, which you can do by adding -Djavafx.runtime.path=/usr/local/javafx/rt to your command line, like this:
java -Djavafx.runtime.path=/usr/local/javafx/rt -jar game.jar
Ugly, but necessary for now. If you run into the problem on your home computer, you'll need to adjust the instructions to correctly reflect the location of your javafx installation on your home computer.
