How to Develop and Run JavaFX Applications Offline

本文介绍如何使用JavaFX及Java部署工具包修改JavaFX应用,以实现断网情况下的执行与测试。通过下载部署工具包文件并进行本地化配置,确保应用能够使用缓存的JavaFX运行时启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 http://javafx.com/docs/techtips/deployment_toolkit/

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"/>

    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. <!-- BEGIN SOURCE CODE -->

    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>
    
    <!-- END SOURCE CODE -->
  7. 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.

    <!-- BEGIN SOURCE CODE -->
    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>
    <!-- END SOURCE CODE -->

    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.

  8. Run your JavaFX application offline.

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


<!-- BEGIN JS-KIT RATINGS -->

<!-- END JS-KIT RATINGS --><!-- BEGIN LEAVE A COMMENT WIDGET -->

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值