Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedReader read and readLine methods hanging the application

807588Apr 23 2009 — edited Apr 24 2009
Dear All,
Please have a look at code.

import java.io.*;
public class Snippet {
public static void transformAction() throws IOException {
Process p = null;
String batchPath="";
batchPath="D:\\ING-TALEND\\JOBS\\FINAL_JOB\\FINAL_JOB_RUN.BAT";
p = Runtime.getRuntime().exec("cmd.exe /c " + "\""+batchPath+"\"");
InputStream is=p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String batOutput = "", line="";
BufferedReader input = new BufferedReader(isr);
//read Code
while (true) {
int nRead = input.read();
char c = (char)nRead;
System.out.print(c);
if (nRead == -1) {
System.out.println("break");
break;
}
}

//readLine code
while ((line = input.readLine()) != null)
{
batOutput = batOutput + " " + line;
System.out.println(batOutput);
}
}

public static void main (String args[]) {
try {
Snippet.transformAction();
} catch (IOException e) {
e.printStackTrace();
}
}
}

I am trying to run a batch file (a talend job) from java and to display input/output to/from the program.
When this batch file called from a command prompt, it gives some exceptions but terminates and return to command prompt.
But when run inside java it got hanged on either readline or read (i tried both).
It shows all the command to be executed on console, but then got hanged.

I guess that it is blocking somewhere.
I searched a lot on web about blocking of readLine and read but haven't got any hint.
Please suggest any remedies.


-Sameer


$@meer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 22 2009
Added on Apr 23 2009
11 comments
4,375 views