I can't capture the console output from Java in FOP process and all my process it doesn't work

一世执手 提交于 2019-12-25 16:27:15

问题


I'm using eclipse + java. I have a method for execute instructions from console. Copied & pasted from internet...

public void viewPDF(String cmd){

    try {

        Process p = Runtime.getRuntime().exec( cmd);

        BufferedReader in = new BufferedReader(  
                            new InputStreamReader(p.getInputStream()));  
        String line = null;  
        while ((line = in.readLine()) != null) {  
            System.out.println(line);  
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

If I execute this code in console cmd all is Ok.

C:\fop-0.93\fop -xml C:\document.xml -xsl C:\document.xsl -pdf C:\document.pdf -d

but if I try to execute from java method it doesn't work, because I can't capture the console output.

The console is waiting and finally I need stop with the red button...

Someone can capture the console output? with other process I can (.bat, file read, etc...) but capture fop output it's impossible.

PD: Sorry for my english... :(

Thanks!!!


回答1:


Finally, instead of running fop from console and capture its output, I have made a java program that creates the pdf.

Someone in this post:

Fop exception when FopFactory.newInstance()

says

FWIW, FOP is just a java library. You dont want to execute it as external process, just use STI API. See for example code.google.com/p/fop-maven-plugin/source/browse/... for an example how to fo that. - Gyro Gearless Aug 30 at 21:27

this is the class I created "inspired": D other:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class ConstructorFOP {

    private InputStream inputStream;

    public void xmlToPdf(String RUTA_XML, String RUTA_XSL, String RUTA_PDF) {

        try {
            System.out.println("Preparing...");

            // Setup directories
            File baseDir = new File("C:");
            File outDir = new File(baseDir, "out");
            outDir.mkdirs();

            File xmlfile = new File(RUTA_XML);
            File xsltfile = new File(RUTA_XSL);
            File pdffile = new File(RUTA_PDF);

            System.out.println("Input XML : " + xmlfile);
            System.out.println("Input XSL : " + xsltfile);
            System.out.println("OutputPDF : " + pdffile);
            System.out.println("");
            System.out.println("Transforming...");

            // configure fopFactory as desired
            final FopFactory fopFactory = FopFactory.newInstance();

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            TransformerFactory tFactory = TransformerFactory.newInstance();

            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup input
            Source src = new StreamSource(xmlfile);

            // Setup Transformer
            Source xsltSrc = new StreamSource(xsltfile);
            Transformer transformer = tFactory.newTransformer(xsltSrc);

            // Make sure the XSL transformation's result is piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);

            ByteArrayOutputStream baos = out;
            inputStream = new ByteArrayInputStream(baos.toByteArray());

            InputStreamAFile(pdffile, inputStream);
        } catch (FOPException e) {
            System.out.println("ERROR DOC 3");
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            System.out.println("ERROR DOC 4");
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            System.out.println("ERROR DOC 5");
            e.printStackTrace();
        } catch (TransformerException e) {
            System.out.println("ERROR DOC 6");
            e.printStackTrace();
        }
    }

    public void InputStreamAFile(File pdffile, InputStream entrada) {
        try {
            OutputStream salida = new FileOutputStream(pdffile);
            byte[] buf = new byte[1024];// Actualizado me olvide del 1024
            int len;
            while ((len = entrada.read(buf)) > 0) {
                salida.write(buf, 0, len);
            }
            salida.close();
            entrada.close();
            System.out.println("Se realizo la conversion con exito");
        } catch (FileNotFoundException e) {
            System.out.println("ERROR DOC 1");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("ERROR DOC 2");
            e.printStackTrace();
        }
    }
}

Thanks!!!



来源:https://stackoverflow.com/questions/25931318/i-cant-capture-the-console-output-from-java-in-fop-process-and-all-my-process-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!