Java程序在批处理文件中的命令完成之前终止 [英] Java program terminates before completion of command in batch file

查看:197
本文介绍了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(等待进程);
process.waitFor();
System.out.println(waiting done); **

} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
// TODO自动生成的catch块
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显示历史记录-r https:// rtc.repo.com:9443/jazz/ -u%userId%-P%pwd%-w1411.201411--component core_as D:\Work\201411\make\main_metadata.xml



输出的是
更改集:
(3129)---- $ Sumit,HARI主元数据更新为部署... 03-Mar-2015 04:09 PM
(3130)---- $ Sumit,HARI固定PartyID问题,签入2015年3月03日01:01 PM
(3131) --- $ Sumit,HARI将项目添加到main_metada xml文件2015年2月26日02:46


解决方案

您的批处理文件正在启动一个新的控制台窗口并终止,即使您使用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 = Runtime.getRuntime()。exec(scmCommand,envp);


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

解决方案

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

这篇关于Java程序在批处理文件中的命令完成之前终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆