我正在使用Java类在Linux机器上执行Perl脚本。 .class
正在成功创build。 但是当我执行这个类的时候,它说exit code successful as sop
,但是我不能够看到Perl的输出或者脚本的执行。 如果我直接执行Perl,它工作正常…
这是Perl脚本:
#!/usr/local/bin/perl print ("Enter the distance to be converted:\n"); $originaldist = <STDIN>; chop ($originaldist); $miles = $originaldist * 0.6214; $kilometers = $originaldist * 1.609; print ($originaldist, " kilometers = ", $miles, " miles\n");
这是我的Java类来调用脚本:
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; class test { public static void main(String[] args) throws IOException { String[] aCmdArgs = { "perl", "-e" , "pl newprg.pl" }; Runtime oRuntime = Runtime.getRuntime(); Process oProcess = null; try { oProcess = oRuntime.exec(aCmdArgs); oProcess.waitFor(); } catch (Exception e) { System.out.println("error executing " + aCmdArgs[0]); } /* dump output stream */ BufferedReader is = new BufferedReader ( new InputStreamReader(oProcess.getInputStream())); String sLine; while ((sLine = is.readLine()) != null) { System.out.println(sLine); } System.out.flush(); /* print final result of process */ System.err.println("Exit status=" + oProcess.exitValue()); return; } }
两个问题:
perl -e pl newprg.pl
实际上不会perl -e pl newprg.pl
执行你的程序,因为它不能解析给定的(非)表达式。 你可能打算使用perl newprg.pl
例如:
try { oProcess = oRuntime.exec(aCmdArgs); PrintWriter writer = new PrintWriter(new OutputStreamWriter( oProcess.getOutputStream())); writer.println(200); writer.close(); oProcess.waitFor(); } catch (Exception e) { System.out.println("error executing " + aCmdArgs[0]); }