I have a batch script which takes input as user name and age and prints both the inputs. I want to write a java program to execute that script and pass the inputs.
I have written the java program using ProcessBuilder. I am also passing the username and age in the process OutputStream but only user name is getting printed and age is missing.
my script(test.bat file) :
@echo off
echo executing test.bat
set /p name=Enter Name:
set /p age=Enter Age :
echo Hi %name%, you are %age% years old.
my java program :
private static void executeInteractiveCommand(String cmd,String ... args){
try{
List<String> command = new ArrayList<String>();
command.add(cmd);
ProcessBuilder builder = new ProcessBuilder(command);
Map<String, String> environ = builder.environment();
final Process process = builder.start();
OutputStream out = process.getOutputStream();
out.write("tester \n\r".getBytes());
out.flush();
out.write("25\n".getBytes());
out.flush();
out.close();
int errCode = process.waitFor();
System.out.println("Process Error Code : "+errCode);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//out.close();
is.close();
System.out.println("Program terminated!");
}catch(IOException ex){
ex.printStackTrace();
} catch(InterruptedException ex){
ex.printStackTrace();
}
}