I'm trying to give to my app self update ability.
It download an JAR from my website and save it as myapp.jar.new
.
After that, I want to launch a command to delete the current version and rename the new one.
This is my code (see the notes):
public void applyUpdateAndRestart() {
Runtime rt = Runtime.getRuntime();
rt.addShutdownHook(new Thread(() -> {
try {
String updateCmd = "restart.cmd";
try (PrintStream ps = new PrintStream(new FileOutputStream(updateCmd))) {
ps.println("@echo off");
// wait for a while to the main process closes and the "myapp.jar" to be writable
ps.println("ping 127.0.0.1 -n 2 > nul");
ps.println("del /q myapp.jar.old");
ps.println("move myapp.jar myapp.jar.old");
ps.println("move myapp.jar.new myapp.jar");
ps.println("java -jar myapp.jar");
}
ProcessBuilder p = new ProcessBuilder();
p.command("cmd", "/c", updateCmd);
System.out.println("Before apply update");
p.start(); // this does not launch
System.out.println("After apply update"); // this prints!
} catch (Throwable e) {
e.printStackTrace(); // this does not occurs!
}
}));
System.exit(0);
}
Why my update.cmd
does not start?
Solved with this approach:
new-myapp.jar
, I launch it with an special argument like this:java -jar new-myapp.jar --do-update
(running the new jar will unlock the current to be overwritten)main
mehtod intercept the argument--do-update
who applies the new jar to current (copy new-myapp.jar myapp.jar
).java -jar myapp.jar
)I think that Klitos comment can solve my problem too, but I solved implementing my previous approach.
On the approach of the question the problem was that the
cmd /c
haven't a console window allocated. Changing the command tocmd /c start
solve the problem too because thestart
command allocate a new console window.