1

I got a very strange problem. I am trying to read the result of a command I am executing. The code never reaches the println-Statement. It is just "hanging up" the program, if the end of the output is reached. No failure and no exception.

My project is a mix of Scala and Java. So it doesn't matter in which language the solution is. I tried in both. The encoding of my project is Cp1252.

Here is my code

var fileScript = Runtime.getRuntime().exec(PathOfScript)
var isr:InputStreamReader  = new InputStreamReader(fileScript.getInputStream())
var in = new BufferedReader(isr)
var line:String = ""
try {
  while ({line = in.readLine();  line!= null}) {
    println("line: "+line)
  }
  println("OUTSIDE !!!");
  in.close();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Does the script process actually complete? Are you sure it's giving output to stdout instead of stderr? – Jon Skeet May 08 '13 at 16:35
  • What does the PathOfScript var contain? Excatly? – gbtimmon May 08 '13 at 16:41
  • The Path is "c:\\nusmv\\nusmv -source script.cmd". The whole output is reachable with the code...it is just "hanging up", if it reaches the end. So it think, that everything is in stdout. – user2363125 May 08 '13 at 16:43

1 Answers1

1

That's strange... my Java version works just fine:

        InputStreamReader isr = new InputStreamReader(new FileInputStream("c:\\anyfile"));
        BufferedReader in = new BufferedReader(isr);
        String line = "";
        try {
          while ((line = in.readLine()) != null) {
            System.out.println("line: "+line);
          }
          System.out.println("OUTSIDE !!!");
          in.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

I think that the problem is in fileScript: if it gives a stream and doesn't close it, you'll never get a null in the while loop. Check that part. Try with a regular file (like I did in my example). If it works, the problem is surely in the fileScript object.

Igor Deruga
  • 1,504
  • 1
  • 10
  • 18
  • Yes, thats right, but i cant use it. I can save the output to a file with an option in the script and read the file. Then everything is fine. The problem is, that it takes a while until the output is written to the file. And i am checking many scripts. Is there maybe a solution to check if the file is already written, without Threads? If i check with file.exists(), i get the feedback false. If i wait for a few milliseconds it seems to be ok. But if the code will be used on slower systems, than i can not be sure, that it works. – user2363125 May 08 '13 at 16:52
  • Can you try it with the following command (or, if you have linux, ls): InputStreamReader isr = new InputStreamReader(Runtime.getRuntime().exec("cmd /c dir").getInputStream()); – Igor Deruga May 08 '13 at 17:24
  • Yes that works. So the problem is the output of my script? I want to thank you in advance Igor, i appreciate your help. – user2363125 May 08 '13 at 17:31
  • No problem, and yes, I think that the problem is in the script you are running. I guess, it's no longer a Java thread, so good luck in further investigation! :) – Igor Deruga May 08 '13 at 18:22
  • It was the script. Thank you! – user2363125 May 08 '13 at 23:13