Java – Executes terminal commands in Java code running within the Cubieboard platform to get output

Executes terminal commands in Java code running within the Cubieboard platform to get output… here is a solution to the problem.

Executes terminal commands in Java code running within the Cubieboard platform to get output

The code I used to run terminal commands in Linux Debian and get output in a java program looks like this:

public static String execute(String command) {
    StringBuilder sb = new StringBuilder();
    String[] commands = new String[]{"/bin/sh", "-c", command};
    try {
        Process proc = new ProcessBuilder(commands).start();
        BufferedReader stdInput = new BufferedReader(new

BufferedReader stdError = new BufferedReader(new

String s = null;
        while ((s = stdInput.readLine()) != null) {

while ((s = stdError.readLine()) != null) {
    } catch (IOException e) {
        return e.getMessage();
    return sb.toString();

The problem now is that it works for normal commands like ls/ and returns the appropriate result. But my goal is to run a command like this:

echo 23 > /sys/class/gpio/export

For example, to activate the gpio pin in the CubieBoard platform.
(The Cubieboard is a mini pc board like the Raspberry Pi.)

Now run this command in the terminal of the system itself, works fine and gives me the correct result. But when I run it from this Java code, I can’t get any results.

The point is, it works and the command executes fine, but I just can’t get the output message of the command!

For example, if a pin was active in the past, then typically it should return a result like this:

bash: echo: write error: Device or resource busy

But when I run this command through the java code above, I get no response.
(Valid again, but I can’t get a response from the terminal!)

When I run the code, both the stdInput and stdError variables in the code have the value null. 🙁

Please help me with my project. This is the only part left 🙁

Thank you.


It is possible that the childProcess did not run to the end

Please try:


And run read stdInput and stdError in other threads before proc.waitFor().


public static String execute(String command) {
        String[] commands = new String[] { "/bin/sh", "-c", command };

ExecutorService executor = Executors.newCachedThreadPool();
        try {
            ProcessBuilder builder = new ProcessBuilder(commands);

            Process proc = builder.start();
            CollectOutput collectStdOut = new CollectOutput(

CollectOutput collectStdErr = new CollectOutput(
            // */

// /*-
             merges standard error and standard output
            Process proc = builder.start();
            CollectOutput out = new CollectOutput(proc.getInputStream());
            // */
             child proc exit code
            int waitFor = proc.waitFor();

return out.get();
        } catch (IOException e) {
            return e.getMessage();
        } catch (InterruptedException e) {
             proc maybe interrupted
        return null;

public static class CollectOutput implements Runnable {
        private final StringBuffer buffer = new StringBuffer();
        private final InputStream inputStream;

public CollectOutput(InputStream inputStream) {
            this.inputStream = inputStream;

         * (non-Javadoc)
         * @see java.lang.Runnable#run()
        public void run() {
            BufferedReader reader = null;
            String line;
            try {
                reader = new BufferedReader(new InputStreamReader(inputStream));
                while ((line = reader.readLine()) != null) {
            } catch (Exception e) {
            } finally {
                try {
                } catch (IOException e) {


public String get() {
            return buffer.toString();

Related Problems and Solutions