- my webspace

- my webspace

Latest Comment

Why I must believe in GOD
Testing time is almost passed..there are many colours in lif...
27/06/12 01:43 More...
By Tarun Shekhawat

Allama Iqbal - Selective verse...
Yahoouj
Really good work about this website was done. Keep trying mo...
07/03/10 15:04 More...
By Roderick

Allama Iqbal - Selective verse...
Great Job
You have dont a great job of collecting these... Even I had ...
25/08/09 01:01 More...
By Sikandar

O ye who don't believe !
It's like Lehman Brothers :grin
11/10/08 10:31 More...
By anurag Chaturvedi

I Protest
@Sikku
Thanks Sikku for the feedback. I never intend to blame, a...
29/07/08 11:06 More...
By Aminur Rashid

Login






Lost Password?
Home arrow Java arrow Provide auto input for batch file
Provide auto input for batch file PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Aminur Rashid   
Sunday, 11 May 2014

One of my known, still in college asked this question on how to run a program that takes dynamic user input for program (typical, "Enter your name" type of commands) multiple times using a batch file, without any real user providing the input. i.e He wanted to run the program x amount of times, and wanted to mimic that a certain user is "typing" those values...
A typical screenshot of such program running in batch could be

D:\localftp>test.bat
D:\localftp>java -jar test.jar
Enter name
Aminur
Type your message


A simple java program (assuming it can't be modified to take Arguments) is as below

package client;

import java.util.Scanner;
/**
 * @author aminur.rashid
 */
public class UserInput {
    public UserInput() {
        super();
    }

    public static void main(String[] args) {
        String a;
        String s;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter name");
        s = in.nextLine();
        System.out.println("Type your message");
        a = in.nextLine();
        System.out.println(s+" says ["+a+"]");
    }
}

A typical batch file, say test.bat (to run the program only once) will have line, assuming the class has been compiled and packaged to test.jar

java -jar test.jar

which stops waiting for user input.

To run the command, where user input can be provided "automatically" using an inputs.txt file

cmd /c test.bat < inputs.txt

where inputs.txt is a text file contains expected input one per line in file e.g

Aminur
This is my message!
and then your batch file won't stop, waiting for user input.

Another way to provide the input messages using command line (temporarily creating the file and deleting could be)

cmd /c echo Aminur^> "%temp%\inputs.tmp" ^& echo My Message^>^> "%temp%\inputs.tmp" ^& (test.bat ^< "%temp%\inputs.tmp") ^& del "%temp%\inputs.tmp"


Suggestions to make this more efficient, most welcome.


StumbleUponDigg This!Bookmark on Delicious

Add as favourites (331) | Quote this article on your site | Views: 2014 | E-mail

Be first to comment this article
RSS comments

Only registered users can write comments.
Please login or register.

Last Updated ( Sunday, 11 May 2014 )
 
< Prev   Next >
Aminur Rashid