5

I'm making an update function for my project, it's working great, until i want it to restart, basically I download the new file and replace it with the old one, and then i want to run it again, now for some reason it doesn't wna run, and i don't get any error...

Here is the complete update class: http://dl.dropbox.com/u/38414202/Update.txt

Here is the method i'm using to run my .jar file:

 String currDir = new File("(CoN).jar").getAbsolutePath();
 Process runManager = Runtime.getRuntime().exec("java -jar " + currDir);
mikerobi
  • 20,527
  • 5
  • 46
  • 42
Olindholm
  • 340
  • 4
  • 16
  • What you are expecting for after or during running this jar? – maks Nov 23 '11 at 21:00
  • Are you expecting to be able to exit the current Java process and have it be replaced by the one you just launched? I'm not sure that is possible given the I/O restrictions Sun mentions on the Process class. – John Haager Nov 23 '11 at 21:09
  • I've seen many Java apps "auto-update" though, so there must be some recommended design for it. – Mike Christensen Nov 23 '11 at 21:10

5 Answers5

1

It's not clear to me, why do you need to run the jar with a call to exec() . Given that you need to run the code in the .jar file from a Java program, you could simply run the main() method as defined in the jar's manifest, and capture its output - wherever that is.

Using exec() is OK when you need to call a program from the underlying operating system, but there are easier ways to do this if both the caller and the callee are Java programs.

Now, if your jar is gonna change dynamically and you need to update your program according to a new jar, there are mechanisms for reloading its contents, for instance take a look ath this other post.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

The JavaDocs for the Process class specifically point out that if you don't capture the output stream of the Process and promptly read it that the process could halt. If this is the case, then you wouldn't see the process that you started run.

John Haager
  • 2,107
  • 1
  • 17
  • 22
0

I think you have to capture the stream like this :

BufferedReader stdInput = new BufferedReader(new InputStreamReader(runManager.getInputStream()),8*1024);

BufferedReader stdError = new BufferedReader(new InputStreamReader(runManager.getErrorStream()));

// read the output from the command

String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
    }
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • I'm not sure you people understood me, I said everything works fine, until when i run it again... so why would i need the input stream? all i wna do is run the .jar after i've replace'd it, – Olindholm Nov 24 '11 at 07:19
0

The exec function doesn't automatically lookup into the PATH to start a process, so you have to pass the complete path for the java binary.

You can do that by using the java.home system property, see this answer: ProcessBuilder - Start another process / JVM - HowTo?

Community
  • 1
  • 1
Diego
  • 4,353
  • 1
  • 24
  • 22
0

No one here seemed to help me, so I went to ask my friend and I had it almost right. It abiously required the string to be an array.

solution:

String[] cmd = {"java", "-jar", currDir};
try {
  Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
  e1.printStackTrace();
}
Olindholm
  • 340
  • 4
  • 16