Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProcessBuilder
package org.pierre.java11tests;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringJoiner;
public class ProcessTest {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("cmd","/C", "dir d:\\temp");
Process p;
String result = "";
try {
p = pb.start();
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringJoiner sj = new StringJoiner(System.getProperty("line.separator"));
reader.lines().iterator().forEachRemaining(sj::add);
result = sj.toString();
p.waitFor();
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
String version = System.getProperty("java.version");
System.out.println("version " + version);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment