Java程序在批处理文件中完成命令之前终止

时间:2015-03-07 16:43:33

标签: java command-line cmd command-line-arguments command-prompt

我正试图从Java程序中删除批处理文件。

批处理文件有一个连接到IBM RTC的命令,然后获取一些大约需要30秒的数据。

但是程序在命令运行后退出而不等待输出。

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();
    }

我尝试添加process.waitFor();,但它没有用。

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 个答案:

答案 0 :(得分:0)

使用start/wstart /wait选项在批处理文件中运行您的程序。

实施例

Start "" /w program options ...

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

  

<强>语法

     

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

     

选项:

     

/W/WAIT启动应用程序并等待它终止。                   (对于内部cmd命令或批处理文件,它运行CMD /K

答案 1 :(得分:0)

您的批处理文件正在启动一个新的控制台窗口并终止,即使您使用start而不是cmd。 还有/ c定义,

/ c执行string指定的命令,然后终止

试试这个,

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

如果这不起作用,试试这个,

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);