使用bat文件从另一个程序调用程序

时间:2015-06-25 05:57:40

标签: java batch-file cmd windows-runtime

我有一个问题,我想用另一个带参数的程序运行我的程序。在第一个程序中,这是代码:

public static void main(String[] args) {
    System.out.println("Starting cmd main");
    String runPath = args[0];
    String path = args[1];
    String flagname = args[2];
    String lockPath = args[3];
    String tampung = args[4];

    String command = "cmd.exe "
            + "/C"
            + " cd C:\\Program Files\\WinRAR"
            +" && rar a -n "+runPath+tampung+".zip "
            + path+ flagname    + " -p"+tampung
            + "\n && rar a -n "+lockPath+tampung+".zip "
            + path+ flagname + " -p"+tampung;

        try {
            Runtime.getRuntime().exec(command);
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
}

当我尝试在cmd中运行时,我编译的程序没有任何错误。但是当我尝试从另一个程序调用时,程序正在运行,但运行时没有给出任何结果。这是第二个程序:

public static void main(String[] args) {
    String runPath = "E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\RUN\\";
    String path = "E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\";
    String flagname = "CMDcommand.txt";
    String lockPath ="E:\\Sementara\\DEVELOPMENT\\SpoolPFileLapBulTBEListener\\FLAG\\RUN\\LOCK\\";
    String tampung = "testing";
    String cmd = "C:\\ProgramCmd\\Start.bat" + " " + runPath + " " + path + " "+ flagname + " " + lockPath + " " + tampung;

    String[] command = { "cmd.exe", "/c", cmd };                                            
    System.out.println("Command: " + cmd);

    try {
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line="";
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        process.waitFor();  
    } catch (IOException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

对于第一个程序中的.bat,我使用它:

java CmdMain %1 %2 %3 %4 %5

当我尝试运行程序时,结果如下:

  

命令:C:\ ProgramCmd \ Start.bat E:\ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ RUN \ E:\ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ CMDcommand.txt E:\ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ RUN \ LOCK \ testing

     

C:\ Users \ Agung Rizkiono \ Documents \ Development \ Workspace2 \ testing2> java CmdMain E:\ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ RUN \ E:\ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ CMDcommand.txt E: \ Sementara \ DEVELOPMENT \ SpoolPFileLapBulTBEListener \ FLAG \ RUN \ LOCK \ testing

我如何尝试修复第二个程序?

0 个答案:

没有答案