public class ReadPerl {
public static void main(String[] args) throws IOException {
StringBuffer resultStringBuffer = new StringBuffer();
String lineToRead = "";
int exitValue = 0;
try {
Process proc = Runtime.getRuntime().exec("perl D:\\myperl.pl");
InputStream inputStream = proc.getInputStream();
BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream));
// save first line
if ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append(lineToRead);
}
// save next lines
while ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append("\r\n");
resultStringBuffer.append(lineToRead);
}
// Always reading STDOUT first, then STDERR, exitValue last
proc.waitFor(); // wait for reading STDOUT and STDERR over
exitValue = proc.exitValue();
} catch (Exception ex) {
resultStringBuffer = new StringBuffer("");
exitValue = 2;
}
System.out.println("exit:" + exitValue);
System.out.println(resultStringBuffer.toString());
}
}
为什么没有反应啊?lineToRead 一直是空值。绝对不是路径问题,D盘有myperl.pl这个文件
Java Perl
------解决方案--------------------
Process proc = Runtime.getRuntime().exec("cmd /c start perl D:\\myperl.pl");或者
Process proc = Runtime.getRuntime().exec("cmd /c perl D:\\myperl.pl");