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 677929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:05:35+00:00 2026-05-14T01:05:35+00:00

This is a followup question to my other question : Run bat file in

  • 0

This is a followup question to my other question : Run bat file in Java and wait

The reason i am posting this as a separate question is that the one i already asked was answered correctly. From some research i did my problem is unique to my case so i decided to create a new question. Please go read that question before continuing with this one as they are closely related.

Running the proposed code blocks the program at the waitFor invocation. After some research i found that the waitFor method blocks if your process has output that needs to be proccessed so you should first empty the output stream and the error stream. I did those things but my method still blocks. I then found a suggestion to simply loop while waiting the exitValue method to return the exit value of the process and handle the exception thrown if it is not, pausing for a brief moment as well so as not to consume all the CPU. I did this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

 public static void main(String[] args) {
  try {
   Process p = Runtime.getRuntime().exec(
     "cmd /k start SQLScriptsToRun.bat" + " -UuserName -Ppassword"
       + " projectName");
   final BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
   final BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
   new Thread(new Runnable() {

    @Override
    public void run() {
     try {
      while (input.readLine()!=null) {}
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }).start();
   new Thread(new Runnable() {

    @Override
    public void run() {
     try {
      while (error.readLine()!=null) {}
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }).start();
   int i = 0;
   boolean finished = false;
   while (!finished) {
    try { 
     i = p.exitValue();
     finished = true;
    } catch (IllegalThreadStateException e) {
     e.printStackTrace();
     try {
      Thread.sleep(500);
     } catch (InterruptedException e1) {
      e1.printStackTrace();
     }
    }
   }
   System.out.println(i);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

but my process will not end! I keep getting this error:

java.lang.IllegalThreadStateException: process has not exited

Any ideas as to why my process will not exit? Or do you have any libraries to suggest that handle executing batch files properly and wait until the execution is finished?

  • 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-05-14T01:05:35+00:00Added an answer on May 14, 2026 at 1:05 am

    Start cmd with the /c switch instead of /k and get rid of the start:

    Process p = Runtime.getRuntime().exec( 
       "cmd /c SQLScriptsToRun.bat" + " -UuserName -Ppassword" 
       + " projectName");
    

    /k tells cmd: “Run that command and then stay open”, while /c says “Run that command and then exit.”

    /k is for interactive use where you want an initializing batch file and afterwards still use the console.

    Your main problem here, however, is that you are creating yet another process by using start. To run a batch file this is totally unnecessary and prevents you from knowing when the batch was run completely, since Java has a reference to the original cmd process you started, not the one you spawned with start.

    In principle, this now looks like the following:

    1. Java program starts
    2. Java program runs cmd and instructs it to run start foo.bat and stay open for interactive input (/k)
    3. Java memorizes the process ID (PID 42) to later reference that process

      • cmd (PID 42) starts
      • cmd (PID 42) runs start foo.bat
      • start foo.bat launches another instance of cmd, since that’s what should happen to run batch files
        • cmd (PID 57005) starts
        • cmd (PID 57005) runs foo.bat
        • cmd (PID 57005) exits (This marks the event you’d like to know about)
      • cmd (PID 42) shows the prompt and obediently waits for input (unbeknownst to them the prompt is never seen by a user and no input will ever come … but cmd (PID 42) waits …)
    4. Java likes to know whether the process is finished and checks PID 42

    5. Yup, it’s still there. Now what?

    What you want (and what above change will do) is:

    1. Java program starts
    2. Java program runs cmd and instructs it to run foo.bat and close after running the command (/c)
    3. Java memorizes the process ID (PID 42) to later reference that process

      • cmd (PID 42) starts
      • cmd (PID 42) runs foo.bat
      • cmd (PID 42) exits
    4. Java likes to know whether the process is finished and checks PID 42

    5. Hooray, the process is gone, the batch file has been run.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup question to one I previously asked: start-program-if-not-already-running-in-java I didn't get
This is a followup question to my other widget-related question . I'd like to
NOTE: This is a followup to my question here. I have a program that
This is a follow-up question to this other one . I am trying to
This is a follow up to my other question . I thought that mylist
This is a follow-up to my other question here: How to read vfat attributes
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a followup to a question I posted yesterday. I thought everything was
As a followup to this question , is it possible to write a single
This is a followup of a previous question I had. I got the very

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.