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
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
while ((s = stdError.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} 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.
Solution
It is possible that the childProcess did not run to the end
Please try:
proc.waitFor()
And run read stdInput and stdError in other threads before proc.waitFor().
Example:
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(
proc.getInputStream());
executor.execute(collectStdOut);
CollectOutput collectStdErr = new CollectOutput(
proc.getErrorStream());
executor.execute(collectStdErr);
// */
// /*-
merges standard error and standard output
builder.redirectErrorStream();
Process proc = builder.start();
CollectOutput out = new CollectOutput(proc.getInputStream());
executor.execute(out);
// */
child proc exit code
int waitFor = proc.waitFor();
return out.get();
} catch (IOException e) {
return e.getMessage();
} catch (InterruptedException e) {
proc maybe interrupted
e.printStackTrace();
}
return null;
}
public static class CollectOutput implements Runnable {
private final StringBuffer buffer = new StringBuffer();
private final InputStream inputStream;
public CollectOutput(InputStream inputStream) {
super();
this.inputStream = inputStream;
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
BufferedReader reader = null;
String line;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
buffer.append(line).append('\n');
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
reader.close();
} catch (IOException e) {
}
}
}
public String get() {
return buffer.toString();
}
}