• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Multiple inputs for Java Runtime

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This topic should give you different approaches for reading inputs in Java. we-ask-user-input-writing
 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic