簡體   English   中英

如何在Ubuntu中從Java執行Perl腳本

[英]How to execute perl script from java in ubuntu

我有一個需要2個參數才能運行的perl腳本。 我目前正在使用Ubuntu,並且通過將目錄更改為perl腳本所在的位置並通過編寫來設法從終端執行perl腳本

perl tool.pl config=../mydefault.config file=../Test

但是,當我嘗試從Java程序運行perl腳本時(我正在使用eclipse),它總是給我消息Command Failure 這是代碼:

Process process;
try {
    process = Runtime.getRuntime().exec("perl /home/Leen/Desktop/Tools/bin/tool.pl config=../mydefault.config file=../Test");
    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());
}

所以,請問我的代碼有什么問題?

您應該在對exec()的調用中將命令與其參數分開 ,例如:

Runtime.getRuntime().exec(new String[] {"perl", "/home/Leen...", "config=...", "file=..."});

使用當前擁有的內容,Runtime會查找一個字面名為 perl /home/Leen/Desktop... ,空格和全部的命令。

當您從終端中運行整個命令,你的shell是足夠聰明,意識到一個空間界定命令的名稱( perl從應傳遞給它的參數)( /home/Leen/Desktop...config=...file=... )。 Runtime.exec()並不那么聰明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM