在Java中运行expect脚本时出错

时间:2012-08-30 07:42:46

标签: java expect

我正在运行java的expect脚本。但我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
        at java.lang.Runtime.exec(Runtime.java:615)
        at java.lang.Runtime.exec(Runtime.java:483)
        at ExpectInJava.main(ExpectInJava.java:24)

我能够手动运行expect脚本并且工作正常。

import java.io.IOException;

public class ExpectInJava {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[]params = new String[] {
                "/runScp.expect",
                "/runScp.expect",
                "root@10.21.21.21:/tmp",
                null,
                ""+22,
                ""+600,
                ""+2405,
                ""+"/var/db/host/privatekeys/"+"newsshcred"
                };

        try {
            Process process = Runtime.getRuntime().exec(params);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("e"+e);
        }

    }

3 个答案:

答案 0 :(得分:4)

从数组中删除null

String[]params = new String[] {
        "/runScp.expect",
        "/runScp.expect",
        "root@10.21.21.21:/tmp",
        "",
        ""+22,
        ""+600,
        ""+2405,
        ""+"/var/db/host/privatekeys/"+"newsshcred"
        };

答案 1 :(得分:1)

null数组中params做什么?虽然ProcessBuilder可以接受一个字符串数组,但它们最好是真正的字符串而不是null,因为它们将作为参数传递给子进程,而底层的API不会像null一样。 (另外,Expect 真的不处理空值。)

答案 2 :(得分:1)

看一下exec方法的documentation

482       public Process exec(String cmdarray[]) throws IOException {
483           return exec(cmdarray, null, null);
484       }

它说明何时抛出这样的异常:

472        * @throws  NullPointerException
473        *          If <code>cmdarray</code> is <code>null</code>,
474        *          or one of the elements of <code>cmdarray</code> is <code>null</code>
475        *

阅读 474