This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Multiple inputs for Java Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Multiple inputs for Java Runtime" Watch "Multiple inputs for Java Runtime" New topic
Author

Multiple inputs for Java Runtime

Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
I have a batch file which will 3 inputs one by one.

Its like 'changepassword.bat' with below inputs:

Old Pwd:
New Pwd:
Confirm Pwd:

User need to enter each input and press enter to enter second input.

Now, I am planning to simulate the same in java program.
For this I am using Runtime.exec();
I am writting inputs to outputStream which is the process.getOutputStream() like below:

out.write(bytes1);
out.write(System.getProperty("line.separator").getBytes());
out.write(bytes2);
out.write(System.getProperty("line.separator").getBytes());
out.write(bytes2);
out.flush();
out.close();

the process is being started and keep on waiting for the input.

I am assuming that the process is taking all my data written to stream as single input like ,
(byte1\nbyte2\nbyte3)
and waiting to either <enter> event or for other inputs.

Please help me solving this issue. or suggest any other approach.
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

Hi,

This topic should give you different approaches for reading inputs in Java. we-ask-user-input-writing


Thanks and Regards,
Nilesh Sahni | nsahni@infocepts.com | www.infocepts.com
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
Hi Sahni,

My question is not reading the input dynamically... its about writting multi line input to the Java Runtime Process.

Thanks
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

ohh I took the question in other way.

In this case, may be you can parameterize you batch file and pass all the inputs as parameters to the batch file.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4167
    
    3

Have you implemented all recommendations in all the sections of When Runtime.exec() won't? If not, first make sure you're consuming the output and error streams.

Even with that, I could make input to a .BAT work only by flush()ing the stream after each output and following that with a Thread.sleep(50). Maybe someone with a better understanding of batch files could explain why and/or suggest a better approach.

luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Multiple inputs for Java Runtime
 
Similar Threads
Html <Head><Title> not showing the title
Overhead IO : The cryptic case of the extra bytes
Wait for Runtime.getRuntime().exec to finish?
how to IO console through MyApplication?
Help with File Output Formatting