Archived
fix(native): handle native image in service start/restart
In GraalVM native images, java.home and java.class.path are null. When starting the daemon process, detect if running as native image using org.graalvm.nativeimage.imagecode property. If so, re-exec the current executable instead of trying to spawn a JVM. Fixes NullPointerException on Windows when running `barn service start`. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
461ebe44ab
commit
7b45fce813
2 files changed
+61
-21
No files matched your search
@@ -113,16 +113,7 @@ public class ServiceRestartCommand extends BaseCommand {
|
||||
}
|
||||
|
||||
private int startService(Path effectiveBarnDir) throws IOException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String classPath = System.getProperty("java.class.path");
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(
|
||||
Path.of(javaHome, "bin", "java").toString(),
|
||||
"-cp", classPath,
|
||||
"com.samsonmedia.barn.Main",
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
ProcessBuilder pb = buildDaemonCommand(effectiveBarnDir);
|
||||
|
||||
if (globalOptions != null && globalOptions.getConfigPath().isPresent()) {
|
||||
pb.command().add("--config");
|
||||
@@ -162,4 +153,33 @@ public class ServiceRestartCommand extends BaseCommand {
|
||||
}
|
||||
return ConfigDefaults.getDefaultBaseDir();
|
||||
}
|
||||
|
||||
private ProcessBuilder buildDaemonCommand(Path effectiveBarnDir) {
|
||||
// Check if running as a native image
|
||||
if (isNativeImage()) {
|
||||
// In native image, re-exec the current executable
|
||||
String executable = ProcessHandle.current().info().command().orElse("barn");
|
||||
return new ProcessBuilder(
|
||||
executable,
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
} else {
|
||||
// Running in JVM, use java command
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String classPath = System.getProperty("java.class.path");
|
||||
return new ProcessBuilder(
|
||||
Path.of(javaHome, "bin", "java").toString(),
|
||||
"-cp", classPath,
|
||||
"com.samsonmedia.barn.Main",
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNativeImage() {
|
||||
// GraalVM sets this property when running as native image
|
||||
return System.getProperty("org.graalvm.nativeimage.imagecode") != null;
|
||||
}
|
||||
}
|
||||
@@ -99,18 +99,9 @@ public class ServiceStartCommand extends BaseCommand {
|
||||
}
|
||||
|
||||
private int startDaemon(Path effectiveBarnDir) throws IOException {
|
||||
// For daemonizing, we re-exec with --foreground and nohup
|
||||
// For daemonizing, we re-exec with --foreground
|
||||
// This is a simplified approach; production would use proper daemon techniques
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String classPath = System.getProperty("java.class.path");
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(
|
||||
Path.of(javaHome, "bin", "java").toString(),
|
||||
"-cp", classPath,
|
||||
"com.samsonmedia.barn.Main",
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
ProcessBuilder pb = buildDaemonCommand(effectiveBarnDir);
|
||||
|
||||
if (globalOptions != null && globalOptions.getConfigPath().isPresent()) {
|
||||
pb.command().add("--config");
|
||||
@@ -186,4 +177,33 @@ public class ServiceStartCommand extends BaseCommand {
|
||||
}
|
||||
return ConfigDefaults.getDefaultBaseDir();
|
||||
}
|
||||
|
||||
private ProcessBuilder buildDaemonCommand(Path effectiveBarnDir) {
|
||||
// Check if running as a native image
|
||||
if (isNativeImage()) {
|
||||
// In native image, re-exec the current executable
|
||||
String executable = ProcessHandle.current().info().command().orElse("barn");
|
||||
return new ProcessBuilder(
|
||||
executable,
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
} else {
|
||||
// Running in JVM, use java command
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String classPath = System.getProperty("java.class.path");
|
||||
return new ProcessBuilder(
|
||||
Path.of(javaHome, "bin", "java").toString(),
|
||||
"-cp", classPath,
|
||||
"com.samsonmedia.barn.Main",
|
||||
"service", "start", "--foreground",
|
||||
"--barn-dir", effectiveBarnDir.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNativeImage() {
|
||||
// GraalVM sets this property when running as native image
|
||||
return System.getProperty("org.graalvm.nativeimage.imagecode") != null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user