i have a .jar file, which I can run on the command line:
java -jar myFile.jar argument1
I want to save the output of this .jar as a String vari
Take a look at ProcessBuilder:
http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html
It effectively creates an operating system process which you can then capture the output from using:
process.getInputStream()
.
The line:
processbuilder.redirectErrorStream(true)
will merge the output stream and the error stream in the following example:
e.g.
public class ProcessBuilderExample {
public ProcessBuilderExample() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "gscale.jar");
pb.redirectErrorStream(true);
pb.directory(new File("F:\\Documents and Settings\\Administrator\\Desktop"));
System.out.println("Directory: " + pb.directory().getAbsolutePath());
Process p = pb.start();
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
for (String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println( line ); // Or just ignore it
}
p.waitFor();
}
}