Question

I have issue using Runtime.getRuntime().exec

String line = "";
String output = "";
Process p = Runtime.getRuntime().exec(new String[]{"dmidecode | grep UUID:"});
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
    output += (line + '\n').trim();
}

input.close();

I test this and is not working

String line = "";
String output = "";
Process p = Runtime.getRuntime().exec("dmidecode | grep UUID");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
    output += (line + '\n').trim();
}
input.close();

I get the next error on linux machine:

java.io.IOException: Cannot run program "dmidecode | grep UUID:": error no such file or directory

But I test the command in the console and I get the result!

dmidecode | grep UUID:=> UUID: 564DAF5F-FBF7-5FEE-6BA4-67F0B12D8E0E

How to get the same result using a Java based Process?

Was it helpful?

Solution

The pipe operator | wont work as this is part of the command shell. Try using a shell to execute the command. Also you may want to use ProcessBuilder for its convenience

ProcessBuilder builder = 
      new ProcessBuilder("bash", "-c", "dmidecode | grep UID:");
builder.redirectErrorStream(true);
Process p = builder.start();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top