Why wont it work...

Ok... So if your not interested in Java ignore what follows.

So the problem can be summarized as follows. I have a call out to the operating system from Java to obtain the value of “vmstat” which returns a value from every “x” seconds based on the refresh rate. To do This I use a command similar to this

Runtime rt = Runtime.getRuntime();
String[] comm = new String[] {"vmstat","1"};
Process proc = rt.exec(comm);

I can then loop around capturing the output and parsing it. Simple.... This approach works on Linux, Mac OS, HP-UX, AIX and Solaris however it fails under Windows. It sits there and simply waits. Now this appears to be a common problem based on the number of postings on it. This article suggests a number of approaches to solve the problem. I’ve tried them all and the good news is that they appear to work for a single atomic call that returns all of its output then exits. However commands such as “vmstat/iostat/sar” etc. return output periodically based on their refresh rates and don’t seem to work at all.

There are plenty of hits in google but no one really seems to suggest a solution. Now Im sure this worked in the past and Im almost certain that I haven’t changed the code.

I’ve tried calling

java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"

Which returns the first line of the directory and then exits.

java myexec cmd /c "c:\\cygwin\\bin\\ls.exe"

simply hangs.... whilst

java myexec cmd /c dir

Works fine.... So my believe its the combination of java and cygwin and the way io is redirected. If anyone has a chance to look at it.... I’ll be very grateful. Code for simple testcase below.

    1: import java.util.*;
    2: import java.io.*;
    3:
    4:
    5: class StreamGobbler extends Thread {
    6:
    7:   InputStream is;
    8:   String type;
    9:
   10:   StreamGobbler(InputStream is, String type) {
   11:     this.is = is;
   12:     this.type = type;
   13:   }
   14:
   15:   public void run() {
   16:
   17:     try {
   18:       InputStreamReader isr = new InputStreamReader(is);
   19:       BufferedReader br = new BufferedReader(isr);
   20:       String line = null;
   21:       while (true) {
   22:         if (is.available() == 0) {
   23:           try {
   24:             Thread.sleep(10);
   25:           } catch (InterruptedException ie) {
   26:           }
   27:           continue;
   28:         }
   29:
   30:         line = br.readLine();
   31:         if (line == null) {
   32:           System.out.println("Error");
   33:         } else {
   34:           System.out.println(line);
   35:         }
   36:
   37:       }
   38:     } catch (IOException ioe) {
   39:       ioe.printStackTrace();
   40:     }
   41:
   42:   }
   43:
   44: }
   45: public class myexec {
   46:
   47:   public static void main(String[] args) {
   48:
   49:     try {
   50:       String osName = System.getProperty("os.name");
   51:       System.out.println(osName);
   52:
   53:       Runtime rt = Runtime.getRuntime();
   54:       Process proc = rt.exec(args);
   55:
   56:       // any error message?
   57:       StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
   58:
   59:       // any output?
   60:       StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
   61:
   62:       // kick them off
   63:       errorGobbler.start();
   64:       outputGobbler.start();
   65:
   66:       // any error???
   67:       int exitVal = proc.waitFor();
   68:       System.out.println("ExitValue: " + exitVal);
   69:     } catch (Throwable t) {
   70:       t.printStackTrace();
   71:     }
   72:   }
   73: }
|