randeepsp
randeepsp

Reputation: 3852

Error while running expect script in Java

I am running an expect script from java. But i am getting the following error:

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)

I am able to run the expect script manually and it works fine.

import java.io.IOException;

public class ExpectInJava {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[]params = new String[] {
                "/runScp.expect",
                "/runScp.expect",
                "[email protected]:/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);
        }

    }

Upvotes: 2

Views: 1885

Answers (3)

CloudyMarble
CloudyMarble

Reputation: 37576

Take alook at the documentation of the exec method:

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

It states when such an Exception is thrown:

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        *

Read the Line 474

Upvotes: 2

Donal Fellows
Donal Fellows

Reputation: 137807

What is that null doing there in the params array? While ProcessBuilder can take an array of strings, they'd better be real strings and not null because they're going to be passed as arguments to the subprocess, and the underlying API for that doesn't like null at all. (Also, Expect really doesn't handle nulls.)

Upvotes: 1

mssb
mssb

Reputation: 878

Remove the null from array

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

Upvotes: 4

Related Questions