0

I have written a code which calls a shell script:

 ProcessBuilder pb2=new ProcessBuilder("/home/abhijeet/sample1.sh");
 Process script_exec = pb2.start();
 pb2.redirectError();

Code works for me , as it executes script.

This script takes two arguements 1: input file 2: seqs , in a pattern like:

 sample1.sh -ip=abc.txt --seqs=20

Shell script is interactive one, which asks for many parameters , so i have changed it's code and i will pass those values as arguements to it. So complete format should be like:

db=abc outformat=1 threads=10 sample1.sh --ip=abc.txt --seqs=20

So how can i execute this script using java? Is there any other way to call a interactive script using java?

3 Answers 3

1

You can try this:

 ProcessBuilder pb2=new ProcessBuilder("/home/abhijeet/sample1.sh --ip=abc.txt --seqs=20");
 Process script_exec = pb2.start();
 OutputStream in = script_exec.getOutputStream();
 in.write("abc".getBytes());
 in.write("1".getBytes());
 in.write("10".getBytes());
 in.flush();
 in.close();

This code writes abc, 1 and 10 to process input.

Sign up to request clarification or add additional context in comments.

5 Comments

Is this code for interactive script ? I thought that might be difficult so I have modified script and i want to directly set those values as argument.
When you call interactive program, it waits for user input. in.write("abc".getBytes()); in.write("1".getBytes()); in.write("10".getBytes()); writes the values as an user would do it.
db output and threds are passed by this but --ip and --seqs still not working .Should i change my script and take those values also as these arguements?
As I understand --ip and --seqs are not user inputs, but program parameters. In such case you need to add them to the command or pass as arguments like fmcato wrote.
Stuck at point that if that interactive script is a long one, and it takes input somewhere in between then how can i know using inputstream that script is prompting for user input?
1

I recommend to use Apache Commons Exec, it helps to run external processes in multi-platform environment.

Here is the tutorial: http://commons.apache.org/proper/commons-exec/tutorial.html

Comments

1

Just pass the arguments in the ProcessBuilder constructor. Like this:

ProcessBuilder pb2=new ProcessBuilder("/home/abhijeet/sample1.sh", "-ip=abc.txt", "--seqs=20");

You can also use a List < String > instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.