如何在ubuntu中从java执行perl脚本

Wah*_*saw 1 java ubuntu perl

我有一个perl脚本需要运行2个参数.我目前正在使用Ubuntu,我设法通过将目录更改为perl脚本所在的位置并通过写入来执行来自终端的perl脚本

perl tool.pl config=../mydefault.config file=../Test
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试从我的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());
}
Run Code Online (Sandbox Code Playgroud)

那么请问我的代码有什么问题?

mat*_*t b 6

您应该将命令与调用中的参数分开exec(),例如:

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

根据您目前拥有的内容,运行时将查找命名 perl /home/Leen/Desktop...空间和命令的命令.

当您从终端中运行整个命令,你的shell是足够聪明,意识到一个空间界定命令的名称(perl从应传递给它(参数)/home/Leen/Desktop...,config=...,file=...).Runtime.exec()并不聪明.