问题描述
我正在使用 eclipse + java.我有一种从控制台执行指令的方法.复制&从网上粘贴...
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();
}
}
如果我在控制台 cmd 中执行此代码一切正常.
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
但是如果我尝试从 java 方法执行它不起作用,因为我无法捕获控制台输出.
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...
有人可以捕获控制台输出吗?使用其他进程我可以(.bat、文件读取等...)但捕获 fop 输出是不可能的.
Someone can capture the console output? with other process I can (.bat, file read, etc...) but capture fop output it's impossible.
PD:对不起我的英语... :(
PD: Sorry for my english... :(
谢谢!!!
推荐答案
最后,我没有从控制台运行 fop 并捕获其输出,而是制作了一个创建 pdf 的 java 程序.
Finally, instead of running fop from console and capture its output, I have made a java program that creates the pdf.
这篇文章中的某人:
FopFactory.newInstance() 时的 Fop 异常
说
FWIW,FOP 只是一个 Java 库.您不想将其作为外部进程执行,只需使用 STI API.请参阅例如 code.google.com/p/fop-maven-plugin/source/browse/... 的示例.- 陀螺无齿轮 8 月 30 日 21:27
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
这是我创建的启发"类:D 其他:
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();
}
}
}
谢谢!!!
这篇关于我无法在 FOP 进程中捕获来自 Java 的控制台输出,并且我的所有进程都不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!