我正在编写代码以从 java 运行 perl 脚本。我提供给 java 程序的命令通过 shell 工作得很好。但是,当提供给运行时的 exec() 方法时,相同的命令会返回退出代码 2。为什么会这样?
我的代码是:
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 );
}
}