I'm writing a Java program with multiple threads. One of the threads is responsible for reading lines from the standard input and parsing them.
Everything works well when running normally, but when the program is run in the background (in Linux) using:
$ java -jar my_jar_file &
my program hangs (at least until brought to foreground).
When running in the background I don't really need the standard input, but I also don't want my program to hang.
I searched for a way to programatically determine if the process is running in the background but could not find it.
Here's the code that reads from standard input:
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = null;
try {
while ((line = br.readLine()) != null) {
parseInputLine(line, br);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As far as I can tell, the program hangs when it reaches the br.readLine()
command.