Issue
I am trying to pass a string to a Linux command using <<<:
cat <<< 'Hello'
While this works perfectly in the terminal, Java does not execute this
String cmd = "cat <<< 'Hello'";
Process p = new ProcessBuilder(cmd.split(" ")).start();
String stderr = IOUtils.toString(p.getErrorStream(), Charset.defaultCharset());
String stdout = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());
System.out.println(stderr);
with an error from terminal:
cat: '<<<': No such file or directory
cat: "'hello'": No such file or directory
Why is that so? Commands without <<<
get executed in the usual manner.
Solution
As others have pointed out, <<<
is a capability of shells, like bash. You could invoke bash from your ProcessBuilder… but really, you don’t need a here-string. You have Java.
Here’s how to pass known input to a command:
String input = "Hello";
ProcessBuilder builder = new ProcessBuilder("cat");
builder.inheritIO();
Process p = builder.start();
try (Writer processInput =
new OutputStreamWriter(p.getOutputStream(), Charset.defaultCharset())) {
processInput.write(input);
}
The inheritIO method of ProcessBuilder will cause the subprocess’s standard output to appear in the Java program’s standard output, and similarly, the subprocess’s standard error will appear in the Java program’s standard error.
If you want to capture the standard output of the process, you can replace inheritIO()
with an explicit call to redirectError:
String input = "Hello";
ProcessBuilder builder = new ProcessBuilder("cat");
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = builder.start();
try (Writer processInput =
new OutputStreamWriter(p.getOutputStream(), Charset.defaultCharset())) {
processInput.write(input);
}
try (BufferedReader processOutput = new BufferedReader(
new InputStreamReader(p.getInputStream(), Charset.defaultCharset()))) {
String line;
while ((line = processOutput.readLine()) != null) {
// ...
}
}
Answered By - VGR Answer Checked By - Gilberto Lyons (WPSolving Admin)