From 4c699a956944d4fad7ffc4bb0572133e6dd3b438 Mon Sep 17 00:00:00 2001 From: Lauri Niskanen Date: Mon, 6 Jan 2025 00:10:08 +0200 Subject: [PATCH] Get command path dynamically for restarting The previous version assumes certain installation location. This version works regardless of where the jar file was installed or if it's just a development build that wasn't really installed at all. --- src/main/java/module-info.java | 1 + src/main/java/org/dpsoftware/NativeExecutor.java | 10 +++++++++- src/main/java/org/dpsoftware/config/Constants.java | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 89cc63ebd..69587e046 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -17,6 +17,7 @@ requires ch.qos.logback.classic; requires org.freedesktop.dbus; requires jdk.incubator.vector; + requires java.management; opens org.dpsoftware to javafx.fxml, javafx.web; opens org.dpsoftware.gui to javafx.fxml, javafx.web; diff --git a/src/main/java/org/dpsoftware/NativeExecutor.java b/src/main/java/org/dpsoftware/NativeExecutor.java index 25d286dc9..4381125a8 100644 --- a/src/main/java/org/dpsoftware/NativeExecutor.java +++ b/src/main/java/org/dpsoftware/NativeExecutor.java @@ -38,8 +38,10 @@ import java.awt.*; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; import java.nio.file.*; import java.util.ArrayList; import java.util.Arrays; @@ -169,9 +171,15 @@ private static void restartCmd(List execCommand) { execCommand.addAll(Arrays.stream(Constants.FLATPAK_RUN).toList()); } else if (NativeExecutor.isSnap()) { execCommand.addAll(Arrays.stream(Constants.SNAP_RUN).toList()); + } else if (System.getProperty(Constants.JPACKAGE_APP_PATH) != null) { + execCommand.add(System.getProperty(Constants.JPACKAGE_APP_PATH)); } else { - execCommand.add(getInstallationPath()); + execCommand.add(System.getProperty(Constants.JAVA_HOME) + Constants.JAVA_BIN); + execCommand.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments()); + execCommand.add(Constants.JAR_PARAM); + execCommand.add(System.getProperty(Constants.JAVA_COMMAND).split("\\s+")[0]); } + if (NativeExecutor.isRunningOnSandbox()) { execCommand.add(Constants.RESTART_DELAY); } diff --git a/src/main/java/org/dpsoftware/config/Constants.java b/src/main/java/org/dpsoftware/config/Constants.java index 2400235ae..c255b9d11 100644 --- a/src/main/java/org/dpsoftware/config/Constants.java +++ b/src/main/java/org/dpsoftware/config/Constants.java @@ -712,5 +712,9 @@ public class Constants { public static final ArrayList HTTP_TOPIC_TO_SKIP_FOR_SATELLITES = new ArrayList<>(Arrays .asList(TOPIC_GLOW_WORM_FIRM_CONFIG, HTTP_SETTING, HTTP_SET_LDR)); public static int GROUP_BY_LEDS = 1; - + public static final String JPACKAGE_APP_PATH = "jpackage.app-path"; + public static final String JAVA_HOME = "java.home"; + public static final String JAVA_COMMAND = "sun.java.command"; + public static final String JAVA_BIN = "/bin/java"; + public static final String JAR_PARAM = "-jar"; }