• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to automate user inputs to a Java program

 
Neelesh A Korade
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We have a Java program which prompts user for 4 or 5 inputs one after the other and then does its computation. The inputs are mostly file names and folder paths. I am trying to figure out a way of automating these inputs. We don't want to (and can not) change the program to read inputs from a file. We only have class files for the program with us.

Is there any way i can automate these user inputs. Say for instance, the inputs would be specified in a text file beforehand and would be read by the java program on each prompt. May be it will require some batch or shell scripting. I am not sure though.

Can someone give some inputs?

-Neelsh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

For most programs you can make the input come from a file at the command line:

java MyClass <filename.ext

The left arrow tells the OS to feed whatever source follows into stdin of the program. See if that does the trick!
 
Neelesh A Korade
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...

Thanks for the response and sorry for the long delay in coming back on this.

Can you please tell me if the input is given on the command line the way you have suggested, what is the way to receive it in the program? For instance, how're the contents of filename.ext going to be read in the Java program?

Thanks
Neelesh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program should read the file one line at a time just as if the user was typing one response at a time. The newline in the file acts just like the enter key for the user. Make your own simple program that just reads lines from stdin and see how it behaves with different files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!