2016-09-26 13 views
0

我正在學習perl語言。 我做了一個簡單的perl腳本(如下圖所示)。使用Java語言在NetBean中以簡單的perl腳本運行

#!/usr/bin/perl 
use strict; 
use warnings; 

print "hi Lala\n"; 

我想在NetBean中使用java語言運行此腳本。 我的代碼如下:

public class javaProgram { 
    public static void main(String[] args) { 
     Process process; 

     try 
     { 
      String testFile = "perl C:\\Strawberry\\perl_tests\\hello_world.pl"; 
      process = Runtime.getRuntime().exec(testFile); 
      process.getOutputStream(); 

      process.waitFor(); 
      if(process.exitValue() == 0) 
      { 
       System.out.println("Command Successful"); 

      } 
      else 
      { 
       System.out.println("Command Failure"); 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println("Exception: "+ e.toString()); 
     } 
    } 

} 

但我得到這個錯誤

異常:java.io.IOException異常:不能運行程序 「perl的C:\草莓\ perl_tests \ hello_world.pl」: CreateProcess錯誤= 2,系統找不到指定的文件

我已將腳本保存爲hello_world.pl,如上所示。所以,我不確定我做錯了什麼。 它是NetBean問題嗎?腳本問題?但是當我使用Strawberry IDE運行腳本時,沒有任何問題或錯誤。

+0

的perl沒有找到!添加完整路徑 – Jens

回答

0

perl找不到。 在你的腳本中你指的是#!/usr/bin/perl這是不存在的Windows路徑。所以你必須添加perl Interpreter的完整路徑。

String testFile = "<path_to_perl>perl C:\\Strawberry\\perl_tests\\hello_world.pl"; 

還添加了正確的路徑在您的perl腳本:

#!<path_to_perl>perl 
use strict; 
use warnings; 

print "hi Lala\n"; 
+0

現在,我在將路徑添加到perl後得到了'Command Successful'。 但我仍然看不到預期的輸出'hi lala' 有什麼建議嗎?謝謝 –

+0

@ Kristin.Y你從來沒有讀過流輸出 – Jens

+0

我把這行代碼'OutputStream outs = process.getOutputStream();輸出爲'Outs:java.io.BufferedOutputStream @ 6e8cf4c6' 爲什麼不是字符串? –