【问题标题】:Perl Script through JAVA ErrorPerl 脚本通过 JAVA 错误
【发布时间】:2014-09-10 06:00:02
【问题描述】:

我正在尝试通过 java 运行这个 perl 脚本。下面是我的脚本

public class Log {
    public static void main(String[] args) throws IOException {
        Process proc =null;
        try
        {
            String[] commandAndArgs = {
                "cmd","/c","C:\\Users\\myscipt.pl"
            };
            proc = Runtime.getRuntime().exec(commandAndArgs);
            int returncode = proc.waitFor();
            if(proc.exitValue() == 0)
            {
                System.out.println("Command Successful");
                try
                {
                    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                    String line="";

                    System.out.println("Process Executed"+returncode);
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else{
                System.out.println("Process Executed"+returncode);
                System.out.println("Command Failure");
            }
        }
        catch(Exception t)
        {
            t.printStackTrace();
            System.out.println("Exception: "+ t.toString());
        }
        finally
        {
            proc.destroy();
        }
    }
}

因此,当我执行此脚本时,它会完美运行。但是一旦我用下面的行替换脚本

"perl","C:\\Users\\myscipt.pl"

它会抛出返回代码 2 错误。那么,我哪里错了?

【问题讨论】:

    标签: java perl


    【解决方案1】:

    使用这个:

    Process proc = Runtime.getRuntime().exec("perl C:\\Users\\myscipt.pl");
    

    【讨论】:

    • 作为数组 "perl","C:\Users\myscipt.pl" 传递和作为字符串传递没有区别 "perl C:\\Users\\myscipt.pl"
    • @Kushal Kumar,你在 PATH 变量中有 perl 吗?
    • @BatScream,是的,我有
    【解决方案2】:

    当您从子进程获得错误返回码时,您无法弄清楚到底出了什么问题,除非您同时读取子进程输入和错误流并对其进行监控。

    重要的是,只有在您要求父进程使用子进程输入/错误流之后才等待进程,因为 waitFor() 的文档说:

    如果需要,使当前线程等待,直到进程 此 Process 对象表示的已终止。该方法返回 如果子进程已经终止,则立即。如果 子进程尚未终止,调用线程将被阻塞 直到子进程退出。

    在子进程返回代码后,您将无法像在if(proc.exitValue() == 0) 循环中那样读取它的流。

    因此,为了实现上述目的,您可以稍微修改如下代码,尽管我还没有阅读子进程的错误流,理想情况下您应该这样做。要包括,您可以参考:

    Catching Perl exception in Java

    修改后的代码,

    Process proc =null;
    try
    {
        proc = Runtime.getRuntime().exec("perl C:\\Users\\myscipt.pl");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line="";
        while ((line = input.readLine()) != null) {
            System.out.println(line);
            }   
        int returncode = proc.waitFor();
        System.out.println("Process Executed"+returncode);
    }
    catch(Exception t)
    {
        t.printStackTrace();
        System.out.println("Exception: "+ t.toString());
    }
    finally
    {   if(proc != null)
        proc.destroy();     
    }
    

    一旦您阅读了子进程的错误流,您就会知道发生了什么错误导致该进程返回该代码。

    【讨论】:

      【解决方案3】:

      所以,我得到了解决方案。而不是仅仅放

      "perl","C:\Users\myscipt.pl"

      我用完整的 perl 路径替换了“perl”,即

      "C:\Users\Perl64\bin\perl.exe","C:\Users\myscipt.pl"

      成功了

      【讨论】:

        猜你喜欢
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 2013-06-18
        • 2013-09-05
        • 2015-10-30
        • 2012-05-31
        相关资源
        最近更新 更多