1

I have used the code below to run cmd commands from java (code adopted from here)

  private static void execute(File file){
   try {
        String[] command =
        {
           "cmd",
        };
        Process p = Runtime.getRuntime().exec(command);
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("cd " + file.getParent());
        stdin.println("gxm -execute " + file.getPath());
        // write any other commands you want here
        stdin.close();
        int returnCode = p.waitFor();
        System.out.println("Return code = " + returnCode);
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

SyncPipe Class:

class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
      istrm_ = istrm;
      ostrm_ = ostrm;
  }
  public void run() {
      try
      {
          final byte[] buffer = new byte[1024];
          for (int length = 0; (length = istrm_.read(buffer)) != -1; )
          {
              ostrm_.write(buffer, 0, length);
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  private final OutputStream ostrm_;
  private final InputStream istrm_;
}

this code works fine and the execution result is a file called COMPLETED, but i have implemented the following method to check for that file to indicate that the execution is finished.

  private static boolean checkFinished(String path)
{
    boolean result = false;
    String directory = path + "\\expts";
    File dir = new File(directory);
    if(dir.isDirectory())
        while(!result)
        {
            for(File f: dir.listFiles())
                if(!f.isDirectory() && "__COMPLETED__".equals(f.getName()))
                {
                    result = true;
                     break;
                }
            if(!result)
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    System.out.println(result);
    return result;

}

but when i call these methods in the main method like this:

           File f = new File("C:\\inputFile.txt");
    execute(f);

    System.out.println(checkFinished(f.getParent()));

I get the following output:

false // printed from the checking
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
.... // rest of cmd output

The problem here is that the checkFinshed method is printing once and print true later on when the file is already there. Whats wrong in the code?

Community
  • 1
  • 1
Sami
  • 7,797
  • 18
  • 45
  • 69

1 Answers1

0

37 views and not even a comment !! Anyways, I changed my checkFinished method to the following and it worked. hope it will benefit someone else.

  private static boolean checkFinished(String path)
{
    boolean result = false;
    String directory = path + "\\expts";
    File dir = new File(directory);
    while(true) 
    {
        try 
        {
            Thread.sleep(3000);
        } catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(dir.isDirectory())
        {
            for(File f: dir.listFiles())
                if(!f.isDirectory() && "__COMPLETED__".equals(f.getName()))
                {
                    result = true;
                    logger.info(" SIMULATOR:  simulation finished ");
                     break;
                }
            if(result)
                break;
        }
    }
    return result;
}
Sami
  • 7,797
  • 18
  • 45
  • 69