-3

I want to run a jar from a java class, how could I do that? Something like:

public static void main(String[] args) {
    Process process = Runtime.getRuntime().exec("java --jar grok.jar");
}

I'd be running this from within my IDE, eclipse. I'm not sure how to use the returned process to check the result of the command. It would also be nice if I could direct the output of the jar to a location of my choosing. This could probably be accomplished using "process.getOutputStream()", but since the jar will launch a blocking process, not sure how that would work?

Thanks

user291701
  • 38,411
  • 72
  • 187
  • 285
  • To direct the "_output of the jar_" to a location of your choosing, you'll probably want to add logic to your "_jarred_" program. For example, you might pass the output location as an argument. Seems to me this is the portable way to implement this. You could do something like `java --jar grok.jar > output-file`, but I don't know if that would work on Windows (like most things it would probably work everywhere else). That said, I'm not sure you can redirect output through `Runtime.getRuntime().exec()`... – jahroy Feb 06 '13 at 22:59

1 Answers1

1

The question is already answered here. Look at this answer for example, it should give you an idea on how to start a process and get the output as an InputStream.

    Process ps=Runtime.getRuntime().exec(new String[]{"java","-jar","A.jar"});
    ps.waitFor();
    java.io.InputStream is=ps.getInputStream();
Community
  • 1
  • 1
unbehagen
  • 113
  • 5
  • 3
    You'd better stop your code sample after the third line. The other ones are plain wrong. That's not how you read everything from an input stream. – JB Nizet Feb 06 '13 at 23:02