我在編寫代碼以從java運行perl腳本。我提供給java程序的命令通過shell可以很好地工作。但是,當提供給Runtime的exec()方法時,相同的命令返回退出代碼2。爲什麼發生這種情況?未在Java中運行的Perl腳本
我的代碼是:
package org.nlp.rishabh;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ReferenceExtractor {
public static void main(String[] args) throws Exception {
String parscitCommand = "/home/rishabh/Desktop/parscit/bin/citeExtract.pl ";
String parscitOptions = "-m ";
String parscitAction = "extract_all ";
String paperPath = "/home/rishabh/Desktop/aas.txt";
String xml = null;
String command = parscitCommand + parscitOptions + parscitAction + paperPath;
System.out.println(command);
try {
Runtime runtime = Runtime.getRuntime();
Process pr = runtime.exec(command);
// Process pr = runtime.exec(new String[] { "perl", parscitCommand, parscitOptions, parscitAction, paperPath });
BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String temp;
StringBuilder sb = new StringBuilder();
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
xml = sb.toString();
int returnVal = pr.waitFor();
System.out.println(returnVal);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(xml);
}
}
我認爲 - 只要Perl腳本是正確的chmod,並且有一個shebang行 - 這應該不是問題。 –