How to add a program to startup with jpackage?
Asked Answered
I

2

8

i am recently looked at jpackage there is any option automatically adds the appilication to startup, For Example consider I have,

App.java

package org.openjfx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        var label = new Label("Hello, JavaFX");
        var scene = new Scene(new StackPane(label), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

module-info.java

module Sample {
    requires javafx.controls;
    opens org.openjfx;
}

Using maven to generate runtime,

mvn javafx:jlink

Then generate the installer,

jpackage --win-dir-chooser --runtime-image ./target/image/ --name Sample-Javafx --module Sample/org.openjfx.App -d ./target/bin/

This all works fine but what i want is to register App.java at startup and start this app after installation is it possible with jpackage or there is any trick inside App.java to achive this ?

Invigorate answered 26/9, 2021 at 13:6 Comment(5)
As far as I know jpackage does not provide such an option.Montparnasse
Thanks for your reply, there is any other option to achive this ?Invigorate
I think not, but maybe someone else knows. Good luck :)Montparnasse
You may want to submit a feature request for this, assuming one doesn't already exist. A potential workaround, if you continue to use jpackage, might be to try and do this within your application when it first starts. For instance, on Windows I'm pretty sure startup apps are determined by simply having a shortcut in a specific folder; not sure about other operating systems.Retaliation
jpackage relies on the wix toolset to build Windows installers. Wix can define services, or add a program to start on boot. It may be possible to customize your build to specify the options you wish so Wix will build the installer you want, but I haven’t enough jpackage experience to know that for sure or provide instructions.Spittle
K
2

To add the program to auto start you need to customize Wix configuration used by jpackage. To do this first run:

jpackage --temp <some directory> ...

Details about jpackage arguments can be found here.

This will create a snapshot of Wix configuration used by your version of jpackage. In that directory you should find main.wxs file, which is the main configuration file of the installer. Create a resources folder and place the copy of main.wxs file in that folder. You can make jpackage use your configuration by adding

--resource-dir resources

After first run you should see no difference, but now, you can customize the Wix installer by changing the main.wxs file in resources directory. You can make the program start automatically by adding an appropriate Windows Registry Key - add the following snippet just before <Feature> tag

<DirectoryRef Id="TARGETDIR">
  <Component Id="AutostartComponent" Guid="{your-unique-uuid}">
    <RegistryKey Root="HKCU" Key="Software\Microsoft\Windows\CurrentVersion\Run">
      <RegistryValue Type="string" Name="$(var.JpAppName)" Value="&quot;[INSTALLDIR]$(var.JpAppName).exe&quot;" />
    </RegistryKey>
  </Component>
</DirectoryRef>

Make sure that your exe file name follows the pattern used in the snippet.

Next, at the end of <Feature> tag add the reference to the created component:

  ... 
  <ComponentRef Id="AutostartComponent"/>
</Feature>

Details about adding registry keys during the installation can be found here.

Katelyn answered 29/3 at 9:18 Comment(1)
This answer was the only one that worked for me. Thank you!Benzidine
M
0

While you are running on Windows, I know how to tackle this on Linux.

JPackage will create a Debian Package. Such a package contains mainly two tar balls: The main one contains the files that need to be installed in the filesystem. The other one contains metadata (what package do we have here?) as well as four scripts, each of them will be run at a specific event:

  • preinst executed before the tar ball gets extracted (installation)
  • postinst executed after the tar ball got extracted (installation)
  • prerm executed before the application will be removed (uninstall)
  • postrm executed before the application was removed (uninstall)

Coming back to your question, all I'd have to do is provide my version of the postinst script that would register the application to autostart following https://docs.oracle.com/en/java/javase/17/jpackage/override-jpackage-resources.html#GUID-1B718F8B-B68D-4D46-B1DB-003D7729AAB6

Maybe there is something similar for the Windows version?

Mckenney answered 19/4, 2022 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.