You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将JavaFX Jar打包为带安装程序的可执行文件并实现开机自启

Hey there! Let's break down how to solve your two key requirements: turning your JavaFX JAR into a standalone executable that works without a JDK installed, and setting it to automatically launch when the system starts up or wakes from sleep.

Step 1: Package Your JavaFX JAR into a Standalone Executable

The official, most reliable way to do this is using jlink (to create a custom JRE tailored to your app) and jpackage (to wrap the JRE and JAR into a native executable). Here's how:

  1. Prerequisites: Make sure you're using Java 11 or later (JavaFX became a separate module starting with Java 11) and have the JavaFX SDK downloaded.
  2. Create a custom JRE with jlink:
    Run this command in your terminal (replace placeholders with your actual paths and module name):
    jlink --module-path "path/to/javafx-sdk/lib;path/to/your/compiled/modules" --add-modules your.app.module.name --output custom-jre
    
    • If your app isn't modular (no module-info.java), you can skip jlink and directly use jpackage with the --add-modules flag to specify required JavaFX modules (e.g., javafx.controls,javafx.fxml).
  3. Build the native executable with jpackage:
    • Windows:
      jpackage --name YourAppName --input ./path/to/your/jar --main-jar your-app.jar --runtime-image ./custom-jre --type exe --win-shortcut --win-menu
      
      This creates an EXE installer that adds a shortcut to your Start Menu and desktop.
    • macOS:
      jpackage --name YourAppName --input ./path/to/your/jar --main-jar your-app.jar --runtime-image ./custom-jre --type app-image --mac-package-name "Your App Name"
      
      This outputs a .app bundle ready to be moved to your Applications folder.
    • Linux:
      jpackage --name YourAppName --input ./path/to/your/jar --main-jar your-app.jar --runtime-image ./custom-jre --type deb
      
      This creates a Debian package (use --type rpm for Red Hat-based systems).
Step 2: Set Up Auto-Launch on Startup/Wake from Sleep

The method varies by operating system—here's how to do it for each major platform:

Windows

Option 1: Startup Folder

  • Press Win + R, type shell:startup, and hit Enter to open the user-specific startup folder.
  • Create a shortcut to your app's EXE file and paste it into this folder. The app will launch when you log in, and it should remain running after waking from sleep.

Option 2: Registry Editor

  • Open the Registry Editor (regedit), navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run.
  • Right-click in the right pane, select New > String Value. Name it after your app, then set its value to the full path of your EXE (e.g., "C:\Program Files\YourApp\YourApp.exe").

For Wake-from-Sleep Reliability

If you need to ensure the app restarts if it closes during sleep, use Task Scheduler:

  • Create a new task, set the trigger to Workstation Unlock (triggers when the system wakes up).
  • Set the action to Start a program and select your app's EXE.

macOS

Basic Login Auto-Launch

  • Open System Settings > General > Login Items.
  • Click the + button, navigate to your .app bundle in the Applications folder, and add it. The app will launch when you log in and stay running after sleep.

Persistent Auto-Launch (With Restart on Crash/Wake)

For more reliability, use launchd:

  1. Create a plist file at ~/Library/LaunchAgents/com.yourcompany.yourapp.plist (replace placeholders with your details):
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.yourcompany.yourapp</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Applications/YourApp.app/Contents/MacOS/YourApp</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
    </dict>
    </plist>
    
  2. Load the plist with this command:
    launchctl load ~/Library/LaunchAgents/com.yourcompany.yourapp.plist
    
    The KeepAlive key ensures the app restarts if it crashes or closes, including after waking from sleep.

Linux

Desktop Environment Auto-Launch

  • Create a .desktop file at ~/.config/autostart/your-app.desktop with this content:
    [Desktop Entry]
    Type=Application
    Name=YourApp
    Exec=/full/path/to/your/executable
    Hidden=false
    NoDisplay=false
    X-GNOME-Autostart-enabled=true
    
    This works for GNOME, KDE, and most other desktop environments.

Systemd Service (For Advanced Control)

If you need the app to restart automatically after crashes or sleep, use a systemd user service:

  1. Create a service file at ~/.config/systemd/user/your-app.service:
    [Unit]
    Description=Your JavaFX Application
    [Service]
    ExecStart=/full/path/to/your/executable
    Restart=always
    [Install]
    WantedBy=default.target
    
  2. Enable and start the service:
    systemctl --user enable your-app.service
    systemctl --user start your-app.service
    
    The Restart=always option ensures the app runs on login and restarts if it stops, even after waking from sleep.

内容的提问来源于stack exchange,提问作者Rocky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:47:23