Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Simulate value into BufferedReader

 
Greenhorn
Posts: 11
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java How do I simulate autowrite to a Buffered reader prompt ?
For example, the BufferedReader says
Enter the name:

here the name Java shouldbe accepted like
Enter the name:Java

through the Java code itself
 
Saloon Keeper
Posts: 14325
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a pipe. First create a new PipedReader and a PipedWriter like this:
Now, you can wrap your BufferedReader around the PipedReader. Anything you write in your PipedWriter should appear in your BufferedReader.
 
Anjali Lamba
Greenhorn
Posts: 11
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I understood your solution(commented in my code above).
Assume running the above program prompts me to give a name in a cmd prompt. Now how do I enter the name through the code?
 
Stephan van Hulst
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can. The command prompt is just that: it provides a means for a human to provide input. If you want a process to provide input, you can either use a pipe within the same process, or you can redirect standard input to accept input from another process' output.

What is the reason you want to do this? Maybe we can suggest an alternative solution.

 
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have a reference to a Process, you can call its getOutputStream() method to get an OutputStream. What can you do with that? Well:

The API Documentation wrote:Output to the stream is piped into the standard input stream of the process represented by this Process object.



So you can send data to the stdin of the process you ran there, which is what you were asking about.

However in your particular example I don't think that rundll32 is going to be asking for any input. If I'm not mistaken, rundll32 starts up another process which runs whatever you told it to start up, and if I'm also not mistaken, your Java code doesn't have any connection with that process.
 
Stephan van Hulst
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm that's pretty cool, I'll have to take a look at that class as well.
 
Paul Clapham
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely. However anybody who wants to use the Process class should read this classic article: When Runtime.exec() won't.
 
Evildoers! Eat my justice! And this tiny ad's justice too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic