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?

1
  • This type of feature is well handled by Java Web Start. Auto-update is just one of the many features it offers (and it works on Windows, OS X & *nix). Commented Mar 11, 2012 at 17:25

3 Answers 3

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]"
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

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.
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.
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!
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.