Desperately After You
Desperately After You

Reputation: 41

how to pass the arguments to the shell command through java?

I am new to this kind of integration of java with Unix.

what i am trying to do is

    String command="passwd";
    Runtime rt=Runtime.getRuntime();
    try {
        Process pc=rt.exec(command);
        try {
            pc.waitFor();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        BufferedReader buf = new BufferedReader(new InputStreamReader(pc.getInputStream()));

        String line = "";

        while ((line=buf.readLine())!=null) {

        System.out.println(line);

        }
        BufferedReader buf1 = new BufferedReader(new InputStreamReader(pc.getErrorStream()));

        String line1 = "";
        while ((line1=buf1.readLine())!=null) {

            System.out.println("Error--"+line1);

            }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        System.out.println("IOException---"+e1.getMessage());
    }

now when i am trying to pass the "passwd" command the Unix environment goes to suspended mode.

I want to know how can i pass the old password ,new password and confirm new password to the shell using the java code.

Upvotes: 1

Views: 644

Answers (2)

Prathap
Prathap

Reputation: 727

There is a utility called expect. If u installed u can pass argument for any thing. So construct as string execute by String ConstructedCommand; Process p=Runtime.getRuntime().exec(ConstructedCommand);

This link will be deserve your need. http://linux.die.net/man/1/expect

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272337

You need to pass it in using the confusing named Process.getOutputStream(). From the doc:

Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object

Note that you need to capture the processes stdout/err simultaneously to avoid blocking. See this answer for more details.

Upvotes: 2

Related Questions