1

I am trying to exceute batch file from a Java program.

The batch file has a command which connects to IBM RTC then gets some data which takes around 30 seconds.

But the program is exiting just after the command is run without waiting for the output.

public static void main(String[] args) {

    final String scmCommand = "cmd /c  D:\\Coverage\\SCMHistory.bat";       
    try {
        Process process = Runtime.getRuntime().exec(scmCommand);
        /*
         * final InputStream in = process.getInputStream(); int ch;
         * while((ch = in.read()) != -1) { System.out.print((char)ch); }
         * final int returnCode = process.waitFor();
         */
        try (final BufferedReader b = new BufferedReader(
                new InputStreamReader(process.getInputStream()))) {
            String line;

            while ((line = b.readLine()) != null) {
                System.out.println(line);
            }
        }
        **System.out.println("waiting for the process");
        process.waitFor();
        System.out.println("waiting done");**

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I have tried adding process.waitFor(); but it didnt work.

set scm_path=D:\Coverage\RTC\jazz\scmtools\eclipse
set userId=ADMIN
set pwd=ADMIN
set repWorkspace="1081"
%scm_path%\scm show history -r https://rtc.repo.com:9443/jazz/ -u %userId% -P %pwd% -w "1411.201411" --component core_as D:\Work\201411\make\main_metadata.xml



Out put of which is 
Change sets:
(3129) ----$ Sumit, HARI"main metadata is updated to deploy ch..." 03-Mar-2015 04:09 PM
(3130) ----$Sumit, HARI" "Fixed PartyID issue, checked in  " 03-Mar-2015 01:01 PM
(3131) ----$ Sumit, HARI"  "adding project to main_metada xml file" 26-Feb-2015 02:46 PM
2
  • You may want to show what you are executing in your batch file. Most of the time batch files start the executable and return, batch does not wait for the process to finish. Commented Mar 7, 2015 at 17:55
  • @faljbour added the batch file and its output in the Question... Commented Mar 8, 2015 at 12:52

2 Answers 2

1

Use start with the /w or start /wait option to run your program in the batch file.

Example

Start "" /w program options ...

Source Start a program, command or batch script (opens in a new window.)

Syntax

START "title" [/D path] [options] "command" [parameters]

Options:

/W or /WAIT Start application and wait for it to terminate. (for an internal cmd command or a batch file this runs CMD /K)

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried the command as final String dosCommand = "cmd /c start /wait"; String finalCommand = dosCommand + URL + component; Process process = Runtime.getRuntime().exec(scmCommand); but doesn't work
0

your batch file is starting a new console window and terminating, even if you use start instead of cmd. also the /c definition,

/c Carries out the command specified by string and then terminates

try this instead,

final String scmCommand = "D:\\Coverage\\SCMHistory.bat";

if this does not work try this,

final String scmCommand = "D:\Coverage\RTC\jazz\scmtools\eclipse\scm"; 
String[] envp = new String[5];
envp[0] = "-r https://rtc.repo.com:9443/jazz/";
envp[1] = "-u ADMIN";
envp[2] = "-P ADMIN";
envp[3] = "-w \"1411.201411\" ";
envp[4] = "--component core_as D:\Work\201411\make\main_metadata.xml";  

Process process = Runtime.getRuntime().exec(scmCommand, envp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.