Python week November Black Friday
This week up to 80% off on Python courses. More info
Black friday is here! Get up to 80 % extra points for free! More info

Discussion: Execute bash command in Java

Activities (2)
Avatar
Daniel L. Royer:11/16/2016 3:15

Is there any way to execute a Linux terminal command in Java? I found Runtime.exec but I'm having a hard time getting the output of the command.

 
Reply 11/16/2016 3:15
Avatar
David Novák
Member
Avatar
Replies to Daniel L. Royer
David Novák:11/16/2016 3:32

You can use ProcessBuilder: http://docs.oracle.com/…Builder.html

You can then use somethink like proc.getInputStream() or proc.getOutputStream() to get output or set input (in case you are running some interactive command).

 
Up Reply 11/16/2016 3:32
Avatar
David Novák
Member
Avatar
David Novák:11/16/2016 3:33

*something

 
Up Reply 11/16/2016 3:33
Avatar
Replies to David Novák
Daniel L. Royer:11/16/2016 7:00

Thanks for reply. I managed to parse the stream using the getInputStream() method:

try {
            Process p = Runtime.getRuntime().exec(commands);
            p.waitFor();
            extractOutput(p.getInputStream(), out, "");
            extractOutput(p.getErrorStream(), out, "[ERR]: ");
        } catch (IOException e) {
            System.err.println("There was a problem starting the command (possibly a typo?): " + e);
        } catch (InterruptedException e) {
            System.err.println("There was an internal problem running the command: " + e);
}

And the extractOutput() method:

private void extractOutput(InputStream stream, Path dest, String prefix) {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line;
        try {
            while ((line = r.readLine()) != null) {
                System.out.println(prefix + line);
                Files.write(dest, (prefix + line + '\n').getBytes("UTF-8"), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
                System.out.println("");
            }
        } catch (IOException e) {
            System.err.println("Couldn't write program's output to the file " + dest.toString() + ": " + e);
        }
 }

However, I've encountered a new problem - I need to execute a command which requires root privileges (sudo).

I've tried to execute this command:

new String[] {"bin/bash","-c","echo password| sudo -S ls"},

Results in: Cannot run program "bin/bash": error=2, No such file or directory

I'm using Debian.

 
Up Reply 11/16/2016 7:00
Avatar
David Novák
Member
Avatar
Replies to Daniel L. Royer
David Novák:11/16/2016 7:40

Well it says where is the problem - "bin/bash/ does not exist. Simple "bash" didn't work? If you want to use absolute path, you need to put slash in the beginning - "/bin/bash".

 
Up Reply 11/16/2016 7:40
Avatar
Replies to David Novák
Daniel L. Royer:11/17/2016 4:51

"/bin/bash" works like a charm! Thanks.

 
Up Reply 11/17/2016 4:51
Avatar
David Novák
Member
Avatar
Replies to Daniel L. Royer
David Novák:11/19/2016 11:27

You are welcome :)

 
Up Reply 11/19/2016 11:27
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

7 messages from 7 displayed.