0

I am making a web application running on Windows, JSP on Apache Tomcat, which is supposed to convert Office files into PDFs. For this I created macros on Word which save files to PDF format and these have been tested. My application runs correctly on the test machine. What I do is something like the following (sorry got no access to the code right now):

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

I am retrieving the standard and error output. In the test machine it works as expected, also in the server de print the arr variable and use it in the command line and it works. But when called from the app, Word opens as a process in the task manager but never ends as expected.

EDIT:

This is the actual code:

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

And this is the implementation of 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();
        }
    }
}
4
  • 1
    How are you retrieving the standard output and error? Could you show us the code that does this? Apr 3, 2012 at 21:25
  • 1
    This is a known problem- if you fail to empty the output streams of the spawned process properly it may never terminate (will wait for buffer space to become available) Apr 3, 2012 at 22:11
  • @Ricardo Garza V. How did you resolved the above issue. Does the above actual code works now ??
    – DIVA
    Feb 21, 2017 at 10:02
  • Sorry This was a project from an old job, and I no longer have access to the code, so I don't remember the fix Feb 21, 2017 at 21:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.