Invoking bash from Java interactively
OK, here' the deal.
As Eric pointed out, prompt is displayed only if input is coming from a terminal, so I needed a pseudo terminal to fool the bash script. I managed to do it by using JPty library.
How to run interactive bash using Java ProcessBuilder API?
This worked well
ProcessBuilder processBuilder = new ProcessBuilder();
try {
processBuilder.command("stty", "echo");
processBuilder.inheritIO();
processBuilder.start().waitFor(1000, TimeUnit.MILLISECONDS);
processBuilder.command("bash", "-i");
processBuilder.redirectErrorStream(true);
processBuilder.inheritIO();
processBuilder.start().waitFor();
processBuilder.command("stty", "-echo");
processBuilder.inheritIO();
processBuilder.start().waitFor(1000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
consumer.accept("exception occurred while starting bash");
consumer.accept(e.getMessage());
}
using which I was able to see what I was typing, but I don't know why it works. if anyone has a better answer please do post it.
run interactive command line application from java
According to the documentation you should be able to redirect the input and output streams. This tells it to use the System.in
/System.out
from the parent process:
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
If you want to write things to the processes's input:
If the source is Redirect.PIPE (the initial value), then the standard input of a subprocess can be written to using the output stream returned by Process.getOutputStream(). If the source is set to any other value, then Process.getOutputStream() will return a null output stream.
Running an interactive shellscript using java
Read the process output stream and check for the input prompt, if you know the input prompt, then put the values to process inupt stream.
Otherwise you have no way to check.
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
BufferedReader b1 = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter w1 = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
String line = "";
String outp = "";
while ((line = b1.readLine()) != null) {
if (line.equals("PLEASE INPUT THE VALUE:")) {
// output to stream
w1.write("42");
}
outp += line + "\n";
}
...
UPD: for your code it should be something like that
ProcessBuilder pb2=new ProcessBuilder("/home/ahijeet/sample1.sh","--ip="+formobj.getUpFile().getFileName(),"--seqs="+seqs);
script_exec = pb2.start();
OutputStream in = script_exec.getOutputStream();
InputStreamReader rd=new InputStreamReader(script_exec.getInputStream());
pb2.redirectError();
BufferedReader reader1 =new BufferedReader(new InputStreamReader(script_exec.getInputStream()));
StringBuffer out=new StringBuffer();
String output_line = "";
while ((output_line = reader1.readLine())!= null)
{
out=out.append(output_line+"/n");
System.out.println("val of output_line"+output_line);
//---> i need code here to check that whether script is prompting for taking input ,so i can pass it values using output stream
if (output_line.equals("PLEASE INPUT THE VALUE:")) {
// output to stream
in.write("42");
}
}
How to launch an interactive command line interface program from Java?
You can try this instead:
String[] commands = { "gnome-terminal", "-x", "-c", "./My_C_Program" };
theProcess = Runtime.getRuntime().exec(commands);
Not sure will it work. Didn't try it.
Maybe you don't have gnome-terminal but something else like x-term. It should be almost the same.
bash interactive script pass input
If you want to redirect the normal standard input of the program, you could use so called "here documents" (see e.g. the BASH manual page):
java -jar script.jar <<EOF
your input here
EOF
That means standard input (a.k.a. stdin
) is redirected and will be the text in the "here document", in this case your input here
.
Related Topics
Do Threads Created in Java Behave Differently on Windows and Linux
Determine Linux Version from Java
Why the Operating System Says It Can't Allocate Memory to Jvm When It Has Enough Memory
Java.Lang.Unsatisfiedlinkerror in Linux
Why Does Java Rmi Keep Connecting to 127.0.1.1. When Ip Is 192.168.X.X
Animation on Transparent-Background Jframe Linux
In Linux, How to Execute Java Jar File with External Jar Files
Java Error When Trying to Run Netlogo Headlessly on a Cluster
Shuffle Multiple Files in Same Order
How to Abort the Installation of an Rpm Package If Some Conditions Are Not Met in Specfile
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Apache/Hadoop/Util/Platformname
Object Header Size in Java on 64Bit Vm with <4Gb Ram
Why Isn't Calling a Static Method by Way of an Instance an Error For the Java Compiler
Java - Io Bound Thread - 1:1 Threading Model