The following example shows how to read the output from and write the input to external process.
This process could be a DOS Batch script process, VB script process or any other process. Please note that
the Batch process's input stream becomes output and output stream becomes input for your java program.
String line; try {
Process proc = Runtime.getRuntime().exec("C:\\ex_proc_in_out.bat");
BufferedReader procInput = new BufferedReader( new InputStreamReader(proc.getInputStream()));
BufferedWriter procOutput = new BufferedWriter( new OutputStreamWriter(proc.getOutputStream()));
//Give input to batch process
procOutput.write("KV");
procOutput.flush();
//Read output from batch process
line = procInput.readLine();
System.out.println("-->" + line);
//Give input to batch process
procOutput.write("Sudhakar");
procOutput.flush();
//Read output from batch process while ((line = procInput.readLine()) != null) {
System.out.println("->" + line);
}
@echo off
set FNAME=
set /P FNAME=Enter your first name:
echo.
echo Welcome %FNAME%
set LNAME=
set /P LNAME=Enter your last name:
echo.
echo Welcome %LNAME%
It gives the following output,
-->Enter your first name:
->Welcome KV
->Enter your last name:
->Welcome Sudhakar