Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:19:38+00:00 2026-06-08T09:19:38+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:19:40+00:00Added an answer on June 8, 2026 at 9:19 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when I run the code below. I have normally used
I have used the code below to read rfid tag values. try { if
I have used below code to attach quckboook events // Subscribe to UI events...
I have some problem with my cfml website. I have used the below code
Little trouble in using css, in the below code, i have used odd and
I have the following code below on my website. It's used to find the
I have used the below code. stop(); this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadingAction); this.loaderInfo.addEventListener(Event.COMPLETE, onLoadedAction); this.loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErroAction); function
I am trying to run a shell from my python program. I have used
I have used code like this: http://msdn.microsoft.com/en-us/library/dw70f090.aspx to access database before when working in
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.