1

I have a big perl script I want to start through my java program. I have extensively searched on the web for all the possibilities and they did not help me anything further.

I made the following java script for starting my commandline perl script.

try {
    String[] command = {"perl", "C:\\Users\\Rick\\Documents\\Perl\\InDelSub_finder_v0.2.0.pl"};//System.getProperty("user.dir")+"\\src\\InDelSub_finder_v0.2.0.pl", "-h"};
    String[] comm = {"-h"};
    System.out.println(Arrays.toString(command));
    System.out.println(Arrays.toString(comm));
    Process p = Runtime.getRuntime().exec(command, comm);
    p.waitFor();
    try{
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = in.readLine())!=null){
            System.out.println(line);
        }
    }catch(IOException e2){
        e2.printStackTrace();
    }
    System.out.println("exitValue = " + p.waitFor());
} catch (IOException e1) {
// TODO Auto-generated catch block
    e1.printStackTrace();
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

I tried to use a String[] and a String to give to the exec(), but in the end both did not work, it keeps giving me a exitValue() = 255. I also tried (as you can see in the commented away section) that I tried multiple ways to access the right file, but this is not what is going wrong. I think it has something to do with that it can not open the program for some reason.

Could anyone please help me with this? I am really lost in this one and I need it to continue my work.

Thank you very much for your time.

4

1 回答 1

2

您正在使用错误的 exec 签名(带有环境变量的签名)。移动你-hcommand数组。

我认为从 Perl 脚本返回的值不取决于您从 Java 调用它的方式。

为了确保你的脚本被执行,只需尝试一个非常简单的脚本——你可以在磁盘上写一些东西来检查它是否被调用,然后你可以尝试捕获一些输出到标准输出。

请注意,这p.waitFor()可能会导致死锁,因为执行的命令会等待您将其输出接收到标准输出,而 waitFor 将等到命令结束。

分析您的 perl 脚本以找出它返回代码 255 的位置。

快速研究给了我信息,当使用函数时 Perl 以代码 255 退出die- 这可能是一个提示。

于 2013-09-25T13:44:41.317 回答