I'm attempting to execute jstack command using Runtime.exec but it seems there is an error but I can't find it out. In addition, I can execute the following command in CMD and it works fine:
C:\Users\bob>"C:\Program Files\Java\jdk1.6.0_18\bin\jstack" 5540 > d:\s.log
Test class full text:
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("\"C:\\Program Files\\Java\\jdk1.6.0_18\\bin\\jstack\" 5540 > d:\\s.log");
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = process.waitFor();
System.out.println("Exited with code '" + exitVal + "'");
} catch (Exception e) {
System.out.println("Error.");
}
}
}
Output:
Usage:
jstack [-l] <pid>
(to connect to running process)
Options:
-l long listing. Prints additional information about locks
-h or -help to print this help message
Exited with code '1'
How can I solve this problem?
Thanks in advance.
exec
and build theProcess
using aProcessBuilder
. Also break aString arg
intoString[] args
to account for arguments which themselves contain spaces. – Andrew Thompson