Incomplete output about Java Runtime.getRuntime().exec(command) - java

I want to build an app like Jenkins terminal:
And I use Java Runtime.getRuntime().exec(command) to execute the command, find out that the output is incomplete.
textshell.sh:
# textshell.sh
echo "wwwwwww";
sleep 2
ls
For example:
When I execute textshell.sh in my mac terminalsh -x testshell.sh , output :
+ echo wwwwwww
wwwwwww
+ sleep 2
+ ls
testshell.sh
but when I execute by java Java Runtime.getRuntime().exec("sh -x testshell.sh") , output:
wwwwwww
testshell.sh
the shell args -x seems useless
How can I fix it?

As #Joachim Sauer points out you are not reading STDERR so miss the lines of echo output from the set -x output. Adjust your code to access the process.getErrorStream() as well.
Alternatively you can switch to ProcessBuilder if wanting to read the error stream merged with the output:
String[]cmd = new String[]{"sh", "-x", "testshell.sh"}
ProcessBuilder pb = new ProcessBuilder(cmd);
// THIS MERGES STDERR>STDOUT:
pb.redirectErrorStream(true);
// EITHER send all output to a file here:
Path stdout = Path.of("mergedio.txt");
pb.redirectOutput(stdout.toFile());
Process p = pb.start();
// OR consume your STDOUT p.getInputStream() here as before:
int rc = p.waitFor();
System.out.println("STDOUT: \""+Files.readString(stdout)+'"');

Related

Get the "watch" command output in java getRuntime().exec

I am trying to get the output from watch free -b and/or top commands in java but when i call it using the getRuntime().exec() method in java, i get an error. Here is what i have tried:
Attempt #1
// This java code
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "watch -t -n 1 free -b");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
int exitVal = process.waitFor();
// Gives this error
Error opening terminal: unknown
Attempt #2
// This java code
Process process = Runtime.getRuntime().exec("/bin/bash");
OutputStream stdin = process.getOutputStream();
PrintWriter pw = new PrintWriter(stdin);
pw.println("watch -t -n 1 free -b < /dev/tty > /dev/tty");
pw.close();
int exitVal = process.waitFor();
// Gives this error
Error opening terminal: unknown
Attempt #3
// This java code
Process process = Runtime.getRuntime().exec("/bin/bash");
OutputStream stdin = process.getOutputStream();
PrintWriter pw = new PrintWriter(stdin);
pw.println("watch -t -n 1 free -b < /dev/tty > /dev/tty");
pw.close();
int exitVal = process.waitFor();
// Gives this error
/bin/bash: line 1: /dev/tty: No such device or address
Also, if i try to pass the command this way to the process builder
ProcessBuilder pb = new ProcessBuilder("/usr/bin/watch -t -n 1 /usr/bin/free -b");
I get this error:
Cannot run program "/usr/bin/watch -t -n 1 /usr/bin/free -b": error=2, No such file or directory
Instead, when i pass the command `top` instead of `watch` i get the following error:
TERM environment variable not set
I think the problems are related because they both work with interactive shell, and also because any other command ( like ping for example ) works perfectly fine. But i am not sure at all.
Is there a way to continuously get the output from a command like watch in java?

bash script not waiting for completion of commands when called from java

I am running the following bash script
scrapy crawl flipkart -a key="$1" -o "$2"flipkart.xml
scrapy crawl myntra -a key="$1" -o "$2"myntra.xml
scrapy crawl jabong -a key="$1" -o "$2"jabong.xml
echo " scrapy completed"
The bash script when executed through the terminal is running as expected as in it completes one execution of the scrapy command then the other but when i try to invoke it through java the same script does not execute the scrapy commands.
It executes the shell script as i am able to read the echo data through the input stream in java.
String command = "/Users/renny/Documents/WorkSpaces/Scrapy/tutorial/tutorial/crawls.sh";
String[] cmd = new String[]{"/bin/sh", command,key,formattedDate};
//Process p = Runtime.getRuntime().exec(cmd);
ProcessBuilder p = new ProcessBuilder(cmd);
Process p2 = p.start();
InputStream error = p2.getErrorStream();
for (int i = 0; i < error.available(); i++) {
System.out.println("" + error.read());
}
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;
System.out.println("Output of running " + command + " is: ");
please suggest me how i can ensure that the scrapy commands get executed.
In a batch file i think we could have called start to ensure that the commands run in separate prompts is there something similar i can do in bash scripts
export PATH=usr/local/bin/scrapy:$PATH
export PATH=/usr/local/bin:$PATH
Adding the export path to the script file solved the issue thanks #RealSkeptic.
By using start, you are asking to start the batch file in the background.
Call Process#waitFor():
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated.
Also you will get the exit value of the subprocess. If successfully code 0, or non-zeroif error exit code`.

Runtime exec on ubuntu

I try to run the wavemon command from java and read the output.
But I can't make the Runtime.exec method work.
The echo command in comments works and prints "hello", but the wavemon command just returns "". Wavemon is installed, I even tried it with it's full path (/usr/bin/wavemon) as an argument.
Nothing works.
// call wavemon
Runtime rt = Runtime.getRuntime();
String[] cmd = { "sh", "-c", "wavemon", "-i wlan1", "-d" };
//String[] cmd = { "sh", "-c","echo hello" };
Process proc = rt.exec(cmd);
proc.waitFor();
// read wavemon output into string
Scanner is = new Scanner(new InputStreamReader(proc.getInputStream()));
StringBuffer buffer = new StringBuffer();
while (is.hasNext()) {
buffer.append(is.nextLine());
}
proc.destroy();
System.out.println(buffer.toString());
The output of the wavemon command starts with an empty line, but since I use a scanner, this should not matter?
$ wavemon -i wlan1 -d
Configured device: wlan1 (IEEE 802.11abgn)
Security: WPA, WPA2, TKIP, CCMP
...
A little detail, this code is used in the Spring framework (spring-boot, tomcat container).
You should not need the "sh -c". Try just using only:
String[] cmd = { "wavemon", "-i","wlan1", "-d" };
Also, you should put the waitFor() after reading the lines from the scanner, as this holds the thread until the process is done. However, the process might not finish until you read it's output. You can get even fancier and read all of the streams on separate threads.
This article has all the details on it http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

Run linux command through java.

I want to run nm command in linux through java.
I tried this code :
command = "nm -l file1.o > file1.txt";
Process p = Runtime.getRuntime().exec(command);
But it's not working, what is wrong with the code?
That is not an executable, it is in fact a shell script.
If you invoke the shell with -c, then you can execute your command:
/bin/sh -c "command > here"
Here's what you need to do:
String command = "nm -l file1.o > file1.txt";
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
The following "simple answer" WON'T WORK :
String command = "/bin/sh -c 'nm -l file1.o > file1.txt'";
Process p = Runtime.getRuntime().exec(command);
because the exec(String) method splits its the string naively using whitespace as the separator and ignoring any quoting. So the above example is equivalent to supplying the following command / argument list.
new String[]{"/bin/sh", "-c", "'nm", "-l", "file1.o", ">", "file1.txt'"};
An alternative to pipe would be to read the stdout of your command, see Java exec() does not return expected result of pipes' connected commands for an example.
Instead of redirecting the output using "> file.txt" you would read whatever the output is and write it to a StringBuffer or OutputStream or whatever you like.
This would have the advantage that you could also read stderr and see if there were errors (like no space left on device etc.). (you can also do that using "2>" using your approach)

running Echo from Java

I'm trying out the Runtime.exec() method to run a command line process.
I wrote this sample code, which runs without problems but doesn't produce a file at c:\tmp.txt.
String cmdLine = "echo foo > c:\\tmp.txt";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmdLine);
BufferedReader input = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
String line;
StringBuilder output = new StringBuilder();
while ((line = input.readLine()) != null) {
output.append(line);
}
int exitVal = pr.waitFor();
logger.info(String.format("Ran command '%s', got exit code %d, output:\n%s", cmdLine, exitVal, output));
The output is
INFO 21-04 20:02:03,024 - Ran command
'echo foo > c:\tmp.txt', got exit code
0, output: foo > c:\tmp.txt
echo is not a standalone command under Windows, but embedded in cmd.exe.
I believe you need to invoke a command like "cmd.exe /C echo ...".
The > is intrepreted by the shell, when echo is run in the cmmand line, and it's the shell who create the file.
When you use it from Java, there is no shell, and what the command sees as argument is :
"foo > c:\tmp.txt"
( Which you can confirm, from the execution output )
You can't just pass "> c:\tmp.txt" to Runtime.exec as part of the command line to make redirection happen. From the Javadocs: "All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream())."
If you want to redirect output to a file, to the best of my knowledge the only way to do that would be to open the file in Java, do getInputStream, and then read from the process's input stream and write to the desired file.

Categories

Resources