【问题标题】:How to execute perl script from java in ubuntu如何在ubuntu中从java执行perl脚本
【发布时间】:2012-09-14 15:24:54
【问题描述】:

我有一个需要运行 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());
}

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

【问题讨论】:

  • 从终端运行时,您检查过命令的存在状态吗? $?。您确定从 Java 代码中的正确目录运行它吗?
  • exec 中的路径。功能似乎错误。在运行脚本之前打印输出 System.properties 并检查运行时环境。或者尝试使用绝对路径:我的意思是 /home/user/....

标签: java perl ubuntu


【解决方案1】:

你应该是separating the command from it's arguments in the call to exec(),如:

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

根据您目前拥有的内容,运行时会查找命令字面意思 perl /home/Leen/Desktop...、空格和所有内容。

当您从终端运行整个命令时,您的 shell 足够聪明,可以意识到空格将命令的名称 (perl) 与应传递给它的参数 (/home/Leen/Desktop..., @987654327 @,file=...)。 Runtime.exec() 没那么聪明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多