4

I would like my Java program to execute a bash script and return the output back to Java. The trick is my script starts some sort of 'interactive session' and I suppose that is why my Java application freezes (Enters an infinite loop I suppose). Here is the code I use to execute the script, I use ProcessBuilder in order to do that. I also tried

Runtime.getRuntime().exec(PathToScript);

It doesn't work either.

public class test1 {
public static void main(String a[]) throws InterruptedException, IOException {

    List<String> commands = new ArrayList<String>();
    List<String> commands1 = new ArrayList<String>();

    commands.add("/Path/To/Script/skrypt3.sh");
    commands.add("> /dev/ttys002");



    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.redirectErrorStream(true);
    try {

        Process prs = pb.start();
        Thread inThread = new Thread(new In(prs.getInputStream()));
        inThread.start();
        Thread.sleep(1000);
        OutputStream writeTo = prs.getOutputStream();
       writeTo.write("oops\n".getBytes());
        writeTo.flush();
        writeTo.close();

    } catch (IOException e) {
        e.printStackTrace();

    }
}
}

class In implements Runnable {

private InputStream is;

public In(InputStream is) {
    this.is = is;
}

@Override
public void run() {
    try {
        byte[] b = new byte[1024];
        int size = 0;
        while ((size = is.read(b)) != -1) {



            System.out.println(new String(b));
        }
        is.close();
    } catch (IOException ex) {
        Logger.getLogger(In.class.getName()).log(Level.SEVERE, null, ex);
    }

}
}

And here is the script I try to execute. It works like a charm when I run it directly from terminal.

#!/bin/bash          
drozer console connect << EOF > /dev/ttys002
permissions
run app.package.info -a com.mwr.example.sieve
exit
EOF
2
  • Have you tried adding bash or /bin/bash before the script file (either in ProcessBuilder or Runtime)? Commented Dec 10, 2015 at 11:29
  • 1
    I just tried, with no results. The problem is that it throws 'command not found' exception. I know those are my custom commands, but why java needs to process each command which is in the script. Let's just run that in terminal. Is there any way to do that? Commented Dec 10, 2015 at 11:36

1 Answer 1

1

You should not be trying to add redirect instructions as part of the command name:

commands.add("/Path/To/Script/skrypt3.sh");
commands.add("> /dev/ttys002");
ProcessBuilder pb = new ProcessBuilder(commands);

Instead, use the redirectOutput method, something like this:

tty = new File("/dev/ttys002");
ProcessBuilder pb = new ProcessBuilder("/Path/To/Script/skrypt3.sh")
    .redirectOutput(ProcessBuilder.Redirect.appendTo(tty))
    .redirectError(ProcessBuilder.Redirect.appendTo(tty))
    .start();

Though, it appears your bash script is already handling the redirection so not sure you need to do that in Java.

See this answer for more info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.