2

I'm writing my Java app a update mechnism. I create a windows batch file,exit my program and the batch file continues to delete my Jar, copy the new one from a remote location, start the jar. My problem: deletion + copy works, BUT - the application won't start. I think the problem is that I don't know how to make Java to execute batch file in separate process tree. when running this: Runtime.getRuntime() I open a child process.

So my question - How can Java execute batch file in separate process tree?

SharonBL
  • 1,735
  • 5
  • 20
  • 28
  • This type of feature is well handled by [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). Auto-update is just one of the many features it offers (and it works on Windows, OS X & *nix). – Andrew Thompson Mar 11 '12 at 17:25

3 Answers3

0

Here is a workaround that uses cmd as the interminent layer:

public class Main {
    public static void main(String[] args) throws Exception {
        Process p = Runtime.getRuntime().exec("cmd /c c:\\test.bat");
    }
}

where test.bat will contain

@echo off
PING 1.1.1.1 -n 1 -w 5000 >nul
java -jar "[path]"
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • Well, if you don't care about being platform-specific, you can try to schedule an event in Windows which executes the jar file long (a minute) after the updater is finished. Even then it is quite a long shot. – Jakub Zaverka Mar 11 '12 at 17:46
0

I believe the answer lies buried in the link that Jakub Zaverka provided. Use start instead of cmd to start the batch file. This will give the batch its own console window.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • It doesn't work. The same problem occurs. I need a trick to start in java a separate process. Otherwise, it's like a child process try to start its father. – SharonBL Mar 12 '12 at 08:07
  • Did you read the "Here" link carefully in Jakub's post? Windows does not have child processes, except for sharing console stdin/stdout/stderr. START creates a new console window with its own streams. You must have some other problem, or some problem with how you used START. Perhaps you should post the failing code so others can better diagnose the problem. For example, If you use START and quote the name of the batch file, then you must also provide a quoted window title as the 1st argument. Empty string ("") is fine. – dbenham Mar 12 '12 at 11:54
  • Hi, I solved the problem. It was due to launch4j - the program i warped my JAR with. Because it allowed one instance, even when I deleted the pre-upgrade file, it didn't allow me to start the new instance from the command line that started under the pre-upgrade program. Now I warp my program without the one instance validation and it works fine! Thank you very much for your help! – SharonBL Mar 12 '12 at 12:57
0

I solved the problem. It was due to launch4j - the program i warped my JAR with. Because it allowed one instance, even when I deleted the pre-upgrade file, it didn't allow me to start the new instance from the command line that started under the pre-upgrade program. Now I warp my program without the one instance validation and it works fine! Thanks everybody for their kind help!

BTW - I validate one instance with this solution - How to allow running only one instance of a Java program at a time?

Community
  • 1
  • 1
SharonBL
  • 1,735
  • 5
  • 20
  • 28