Running bash from Java

I want to do something in this direction.

Process shell = Runtime.getRuntime().exec("/bin/bash"); 

Then I want to use threads for the shell process to talk to the bash shell. However, this does not seem to work at all, and it completely drinks me.

I found a link that seems to be talking about the same problem. Why is this exactly happening and are there better solutions than those indicated in the link?

+1
source share
1 answer

You may need to clear your entries from the JVM to the child process to make sure it receives its input. IIRC I did not need to do this on Windows, but it did on Linux. I also ran into problems when I had to force the child process to drop records, so the JVM would see them too.

Also, make sure that you have JVM threads that are read from stdout and stderr before doing anything, if one of these buffers is full, it can block the process. This is a huge problem for Windows. You will need only one thread if you use parameters to combine threads when the process starts.

Also, your example (above) does not have a new line, will bash not require one? e.g. "touch blah \ n"

+2
source

Source: https://habr.com/ru/post/1433455/


All Articles