当我传递的参数超出比较时,ProcessBuilder无法正常工作

时间:2016-04-20 13:09:24

标签: java processbuilder

当我尝试执行Beyond compare exe时。

我无法传递左侧和右侧文件的参数

public static void exceutecmd() throws ServletException, IOException {
    boolean is64bit = false;

    if (System.getProperty("os.name").contains("Windows")) {
        is64bit = (System.getenv("ProgramFiles(x86)") != null);
    } else {
        is64bit = (System.getProperty("os.arch").indexOf("64") != -1);
    }

    String bin = "C:\\Optum Symmetry Suite\\Symmetry 9.1\\bin";
    String File1 = " C:\\Optum Symmetry Suite\\Symmetry 9.1\\bin\\1.TXT";
    String File2 = " C:\\Optum Symmetry Suite\\Symmetry 9.1\\bin\\2.TXT";

    System.out.println(bin);
    String exe = bin;
    //String configfile = defaultsvaluesSym.get("xml file path");
    //System.out.println(configfile);
    StringBuilder sbd = new StringBuilder();
    if (is64bit) {
        exe = File.separator + "/BC2.exe";
    } else {
        exe = File.separator + "/BC2.exe";
    }

    ****ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \""
            + bin + "\" && \"" + bin + exe +"\"",File1,File2);****

    builder.redirectErrorStream(true);
    Process p = null;
    try {
        p = builder.start();
    } catch (IOException e) {

        e.printStackTrace();
    }
    BufferedReader r = new BufferedReader(new InputStreamReader(
            p.getInputStream()));
    String line = null;
    while (true) {
        try {
            line = r.readLine();
            sbd.append(line);
        } catch (IOException e) {

            e.printStackTrace();
        }
        if (line == null) {
            break;  
        }
    }
    System.out.println(sbd.toString());

}

我能够执行超越比较的exe但不能传递参数。

请帮我解决问题

2 个答案:

答案 0 :(得分:2)

删除命令的“cd”部分。您正在使用完整路径调用程序并传递参数的完整路径。

exe = File.separator + "/BC2.exe";

不需要字符'/',因为您已添加了File.separator。

答案 1 :(得分:0)

public void exceutecmd(HttpServletRequest request,             HttpServletResponse响应,String fileName1,String fileName2)抛出ServletException,IOException {         boolean is64bit = false;         if(System.getProperty(“os.name”)。contains(“Windows”)){             is64bit =(System.getenv(“ProgramFiles(x86)”)!= null);         } else {             is64bit =(System.getProperty(“os.arch”)。indexOf(“64”)!= -1);         }

    String bin = "C:\\Optum Symmetry Suite\\Symmetry 9.1\\bin";

    System.out.println(bin);
    String exe = bin;
    // String configfile = defaultsvaluesSym.get("xml file path");
    // System.out.println(configfile);
    StringBuilder sbd = new StringBuilder();
    if (is64bit) {
        exe = File.separator + "BC2";
    } else {
        exe = File.separator + "/BC2.exe";
    }

    /*ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \""
            + bin + "\" && \"" + bin + exe + "\"");*/

    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \""
            + bin + "\" && \"" + bin + exe + "\""+" \""+fileName1+"\""+" \""+fileName2+"\"");
    builder.redirectErrorStream(true);
    Process p = null;
    try {
        p = builder.start();
    } catch (IOException e) {

        e.printStackTrace();
    }
    BufferedReader r = new BufferedReader(new InputStreamReader(
            p.getInputStream()));
    String line = null;
    while (true) {
        try {
            line = r.readLine();
            sbd.append(line);
        } catch (IOException e) {

            e.printStackTrace();
        }
        if (line == null) {
            break;
        }
    }
    System.out.println(sbd.toString());
    /*
     * RequestDispatcher rd =
     * request.getRequestDispatcher("runAccGrouper.jsp");
     * rd.forward(request, response);
     */
}