我无法捕获控制台输出从Java在FOP过程和所有我的过程

我无法捕获控制台输出从Java在FOP过程和所有我的过程

本文介绍了我无法捕获控制台输出从Java在FOP过程和所有我的过程,它不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse + java。我有一个从控制台执行指令的方法。复制&

  public void viewPDF(String cmd){

try {

进程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();
}
}



如果我在控制台cmd中执行这个代码。

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

但如果我尝试执行java方法它不工作,因为我不能捕获控制台输出。



控制台正在等待,最后我需要停止红色按钮...



有人可以捕获控制台输出?与其他进程我可以(.bat,文件读取等...)但捕获fop输出它是不可能的。



PD:对不起我的英语... :(



谢谢!!!

解决方案


这篇文章中的某人:



时Fop异常





FWIW,FOP只是一个java库,你不想将它作为外部进程执行,只需使用STI API。示例代码.google.com/p/fop-maven-plugin/source/browse / ...一个例子如何fo。 - Gyro Gearless Aug 30 at 21:27



这是我创建的类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 ...);

//设置目录
文件baseDir = new File(C:);
文件outDir =新文件(baseDir,out);
outDir.mkdirs();

文件xmlfile =新文件(RUTA_XML);
文件xsltfile =新文件(RUTA_XSL);
文件pdffile =新文件(RUTA_PDF);

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

//根据需要配置fopFactory
final FopFactory fopFactory = FopFactory.newInstance();

FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
//根据需要配置foUserAgent

ByteArrayOutputStream out = new ByteArrayOutputStream();

TransformerFactory tFactory = TransformerFactory.newInstance();

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

//设置输入
源src = new StreamSource(xmlfile);

//安装Transformer
源xsltSrc = new StreamSource(xsltfile);
Transformer transformer = tFactory.newTransformer(xsltSrc);

//确保XSL转换的结果通过管道传递到FOP
结果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();
}
}
}

p>

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!!!

解决方案

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!!!

这篇关于我无法捕获控制台输出从Java在FOP过程和所有我的过程,它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:42