How to Develop and Run JavaFX Applications Offline

This article describes how to use the JavaFX and Java deployment toolkits to modify your JavaFX application to enable execution and testing when your computer is disconnected from the network. The deployment toolkit provides JavaScript functions that automatically generate the HTML required to deploy Java Web Start applications. The deployJava.js file is the Java deployment tookit and the dtfx.js file is the JavaFX deployment tookit.

In the following steps, you download and edit the JavaFX deployment toolkit to enable offline execution of your JavaFX application.

  1. Download the following files to your computer:

    • http://java.com/js/deployJava.js
    • http://dl.javafx.com/dtfx.js
    • http://dl.javafx.com/javafx-loading-100x100.gif
    • http://dl.javafx.com/javafx-loading-25x25.gif

    The deployJava.js file detects the JRE installation and version, installs the latest JRE version, and enables web pages to run Web Start programs.

  2. Save the files to a local directory on your computer. For Windows create and use c:\offline. For example:

    • c:\offline\deployJava.js
    • c:\offline\dtfx.js
    • c:\offline\javafx-loading-100x100.gif
    • c:\oflline\javafx-loading-25x25.gif

  3. Edit the dtfx.js file to reference the files that you saved to your local computer in step 2:

    • Replace the reference to http://java.com/js/deployJava.js with file:/c:/offline/deployJava.js.
    • Replace the reference to http://dl.javafx.com/javafx-loading-100x100.gif with file:/c:/offline/javafx-loading-100x100.gif.
    • Replace the reference to http://dl.javafx.com/javafx-loading-25x25.gif with file:/c:/offline/javafx-loading-25x25.gif.

  4. Execute the following command while your computer is connected to the network. This command ensures that the latest JavaFX runtime is installed in your local cache.

    • javaws -import http://dl.javafx.com/javafx-cache.jnlp

    On executing this command, you will see the Java splash screen followed by a progress dialog box if any downloading occurs. When the downloading is completed the command will exit silently, indicating that you now have the latest JavaFX runtime cached. If an error is encountered, an error dialog box appears.

  5. Disconnect your computer from the network.

  6. Ensure that the JNLP file for your JavaFX applet or application uses the following <extension> tag for its JavaFX runtime:

    <extension name="JavaFX Runtime" href="http://dl.javafx.com/1.2/javafx-rt.jnlp"/>

  7. This is the default <extension> tag that is generated by the NetBeans IDE. This enables your application to be launched offline by using the cached JavaFX runtime.

    The NetBeans IDE regenerates an HTML file and JNLP a file for your JavaFX application and overwrites the changes that you made to the files that you downloaded from http://dl.javafx.com. Your changes might also be overwritten when you download an updated version of the deployment toolkit. Therefore, you must make a copy of these offline-specific HTML and JNLP files and navigate to them directly when running offline. Remember to change the JNLP link in each offline JNLP file that points to the offline copy. The JNLP file is essential for your application to function on Windows.

    The following sample uses code from the the RippleEffect.jnlp file of the RippleEffect JavaFX demo.

    Source Code
    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" codebase="file:/Users/username/Documents/localdirectory/RippleEffect/dist/" href="RippleEffect.jnlp">
      <information>
        <title>RippleEffect</title>
        <vendor>JavaFX Samples Team</vendor>
        <homepage href=""/>
        <description>RippleEffect</description>
        <offline-allowed/>
         <shortcut>
           <desktop/>
         </shortcut>
      </information>
      <resources>
       <j2se version="1.5+"/>
       <extension name="JavaFX Runtime" href="http://dl.javafx.com/1.2/javafx-rt.jnlp"/>
       <jar href="RippleEffect.jar" main="true"/>
      </resources>
      <application-desc main-class="rippleeffect.Main"></application-desc>
    </jnlp>
    
  8. In your application's HTML page, change the reference of dtfx.js to access your local copy. For example:

    Original reference: <script src="http://dl.javafx.com/1.2/dtfx.js"></script>

    Reference to access your local copy: <script src="file:/c:/offline/dtfx.js"></script>

    This code sample shows the RippleEffect.html file after editing to reference the local version of the dtfx.js script on your computer. The javafx() function is defined in deployjava.js file of the deployment toolkit. This function deploys your application and is used in the application's HTML file to ensure that you have the correct version of Java software and the JRE software installed. You may also specify a custom splash screen to be displayed while your application is loading.

    Source Code
    <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>RippleEffect</title> </head> <body> <h1>RippleEffect</h1> <script src="file:/c:/offline/dtfx.js"></script> <script> javafx( { archive: "RippleEffect.jar", width: 370, height: 450, code: "rippleeffect.Main", name: "RippleEffect" loading_image_url: "http://yourwebsite.com/pictures/companylogo.gif",
    loading_image_height: "120",
    loading_image_width: "80" } ); </script> </body> </html>

    You can also include a custom splash screen to be displayed while your application is loading. Type the location of your custom splash screen image by using loading_image_url. You should specify the dimensions of the image by using loading_image_height and loading_image_width. If the dimensions are not specified, the standard 100x100 or 25x25 pixel size will be used.

  9. Run your JavaFX application offline.

  10. For tips on enhancing the performance of your JavaFX application, see the article, How to Improve JavaFx Application Startup Time.


Rate This Article
Discussion

We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

 

English
日本語
한국어
简体中文
русский
Português do Brasil