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.