Java Runtime.getRuntime().exec()未按预期工作

Java Runtime.getRuntime().exec()未按预期工作,java,process,runtime.exec,Java,Process,Runtime.exec,我试图从文件系统上的html文件中读取由phantomjs创建的pdf。我已经做了以下工作 process = Runtime.getRuntime().exec(phantomLocation + scriptLocation + inputFile + " " + destinationFileString); process.waitFor(); 我正在指定phantomLocation、js脚本位置、inputtml和destinationflesting(

我试图从文件系统上的html文件中读取由phantomjs创建的pdf。我已经做了以下工作

       process = Runtime.getRuntime().exec(phantomLocation + scriptLocation + inputFile + " " + destinationFileString);
       process.waitFor();
我正在指定phantomLocation、js脚本位置、inputtml和destinationflesting(要生成和提供的pdf)

我正在编写以下servlet代码来读取生成的pdf并作为响应发送

        InvokePhantom phantom = new InvokePhantom(inputHTMLFileName, destinationFile);
        process.create();//call the above piece of code
                //Set the response headers
                response.setContentType("application/pdf");
                String headerKey = "Content-Disposition";
                String headerValue = String.format("attachment; filename=\"%s\"", attchmentName);
                response.setHeader(headerKey, headerValue);

                //For debugging
                File file = new File(destinationFile);
                System.out.println("destinationFile exists = " + file.exists());

                //Write to outputStream
                fileInputStream = new FileInputStream(destinationFile);
                outputStream = response.getOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

但是由
phantomjs
生成的pdf文件不完整。当从命令行运行时,phantomjs正确地创建了pdf(从相同的html)。但是当从java代码调用时,它不能正常工作。如何解决此问题?

问题似乎是您试图执行一个以单个字符串形式包含参数的命令。您应该使用如下内容:

String[] cmdArray = new String[]{phantomLocation,scriptLocation,inputFile,destinationFileString};
Process process = Runtime.getRuntime().exec(cmdArray);
process.waitFor();

希望这有帮助。

您能发布需要修改的完整命令吗executed@Sanjeevphantomjs脚本?由
phantomLocation+scriptLocation+inputFile+创建的命令+DestinationListring
以及如何执行命令-line@SanjeevD:\Docs\phantomjs-1.9.7-windows\phantomjs.exe D:\Docs\screenshot.js C:\Users\AppData\Local\Temp\1404368319410387378284269747.html C:\Users\AppData\Local\Temp\1404368319410.pdf从命令行运行时看起来很好,但从java代码运行时不好。尝试读取进程的错误流和可能的错误/输出的输出流我正在获取退出值-1,使用changesTry读取进程的错误流/输出流以获得更多信息