进程挂起java进程生成器启动

时间:2012-04-03 20:03:29

标签: java

我正在使用Windows上运行的Web应用程序,Apache Tomcat上的JSP,它应该将Office文件转换为PDF。为此我在Word上创建了将文件保存为PDF格式的宏,这些已经过测试。我的应用程序在测试机器上正确运行。 我所做的就是以下内容(抱歉现在无法访问代码):

String arr[3] = {wordPath,macroName,fileToconvertPath};
new ProcessBuilder(arr).start();

我正在检索标准和错误输出。在测试机器中它按预期工作,也在服务器中打印arr变量并在命令行中使用它并且它可以工作。 但是当从应用程序调用时,Word将作为任务管理器中的进程打开,但永远不会按预期结束。

编辑:

这是实际代码:

    public boolean changeFormatMSOffice(String inF, String WORD) {
        System.out.println("changeFormatMSOffice(" + inF + "," + WORD + ")");
        String macro = "";
        ArrayList<String> wordArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_WORD.replace(" ", "").split(",")));
        ArrayList<String> excelArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_EXCEL.replace(" ", "").split(",")));
        ArrayList<String> ppArr = new ArrayList<String>(java.util.Arrays.asList(TO_PDF_PP.replace(" ", "").split(",")));
        String extension = inF.substring(inF.lastIndexOf(".")).replace(".", "").trim();
        BufferedWriter out;
        String cmdArr[] = new String[3];
        List<String> cmdList = new ArrayList<String>();
        cmdList.add(WORD);
        cmdArr[0] = WORD;
    String saveFile = "";
    if (wordArr.contains(extension)) {
        macro = "/msaveAsPDF";
        cmdList.add(macro);
        cmdList.add(inF);
        //cmdArr[1] = "/m" + macro;
        cmdArr[2] = inF; // aplica conversion sobre el archivo al que apunta inF
    } else if (excelArr.contains(extension) || ppArr.contains(extension)) {
        if (excelArr.contains(extension)) {
            macro = "/mSaveXLSAsPDF";
        } else {
            macro = "/msavePPTAsPDF";
        }
        cmdList.add(macro);
        int fileNum = 0;
        saveFile = "\"" + DOCUMENTS_PATH + (DOCUMENTS_PATH.substring(DOCUMENTS_PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
        while (new File(saveFile).exists()) {
            fileNum++;
            saveFile = "\"" + DOCUMENTS_PATH + (DOCUMENTS_PATH.substring(DOCUMENTS_PATH.length() - 1).equals(File.separator) ? "" : File.separator) + fileNum + ".txt\"";
        }
        try {
            out = new BufferedWriter(new FileWriter(saveFile));
            out.write(inF);
            out.close();
            cmdList.add(saveFile);
            cmdArr[2] = saveFile; // aplica converion sobre la ruta que esta escrita en la direccion del saveFile
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    cmdArr[1] = macro; //macro a usar
    String cmd = "\"" + cmdArr[0] + "\" " + cmdArr[1] + " \"" + cmdArr[2] + "\"";
    System.out.println(cmd);
    try {
        ProcessBuilder proc = new ProcessBuilder(cmdList);
        System.out.println("PreWaitForList");
        Process pro = proc.start();
        StreamGobbler g1 = new StreamGobbler("stdin", pro.getInputStream());
        StreamGobbler g2 = new StreamGobbler("stderr", pro.getErrorStream());
        g1.start();
        g2.start();
        pro.waitFor();
        //Process proc = Runtime.getRuntime().exec(cmdArr);
        //proc.waitFor();
        System.out.println("AfterWaitFor");
        try {
            if (!saveFile.equals("")) {
                new File(saveFile).delete();
            }
        } catch (Exception e) {
        }
        return true;
    } catch (Exception e) {
        System.err.println(e.toString());
        return false;
    }
}

这是StreamGobbler的实现:

public class StreamGobbler implements Runnable {

    String name;
    InputStream is;
    Thread thread;

    public StreamGobbler(String name, InputStream is) {
        this.name = name;
        this.is = is;
    }

    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            while (true) {
                String s = br.readLine();
                if (s == null) {
                    break;
                }
                System.out.println("[" + name + "] " + s);
            }

            is.close();

        } catch (Exception ex) {
            System.out.println("Problem reading stream " + name + "... :" + ex);
            ex.printStackTrace();
        }
    }
}

0 个答案:

没有答案