0

I have problem with running FOP from Java program. I use Java 1.8_25.

Run commands:

String[] cmd4 = {"java", "-jar", rootDir+"build"+ FileSeparator +"fop.jar", "-c", rootDir+"lib" + FileSeparator + "conf" + FileSeparator + "myfop.xconf", pathToFOP.getCanonicalPath(), outputFile.getCanonicalPath()};
System.out.println(Arrays.toString(cmd4));
Process p = Runtime.getRuntime().exec(cmd4);
p.waitFor();
java.io.InputStream is=p.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);
System.out.println(new String(b));

When I generate in program relatively short output, FOP proceeds normally and generates .pdf file.

When I generate bigger output (but still on one A4 page) FOP process executes, creates empty .pdf file and waits. When I kill the main program, FOP saves .pdf correctly. But I want to have .pdf file converted while main program is running (it also after that opens generated .pdf file)

No error is displayed

output of program:

[java, -jar, C:\JForm\build\fop.jar, -c, C:\JForm\lib\conf\myfop.xconf, C:\Users\Phoros\Documents\test29x.pdf.fop, C:\Users\Phoros\Documents\test29x.pdf]

Thanks for any help.

d3im
  • 323
  • 2
  • 4
  • 18
  • 2
    Have you considered calling FOP directly from your application? – Steve C Dec 03 '14 at 11:20
  • Same question: http://stackoverflow.com/questions/25931318/i-cant-capture-the-console-output-from-java-in-fop-process-and-all-my-process-i?rq=1 – Thilo Dec 03 '14 at 11:35
  • Please don't update the question with rather unrelated followups. You can open a new thread about your font problem. – Thilo Dec 04 '14 at 02:14
  • 1
    OK, sorry. Now everything works fine, fonts also. [another thread on stackoverflow](https://stackoverflow.com/questions/17745133/load-a-font-from-jar-for-fop) – d3im Dec 04 '14 at 13:16

1 Answers1

0

It is not so easy in Java to read output from an external process once that output does not fit into a single buffer anymore. You then have to create a separate thread to read away that output otherwise the process cannot write more and will just block and never finish (which is what you are seeing).

You could work on fixing that, but since FOP is a Java library, it should be easier to directly call the Java API, without the external process.

Another idea would be to involve a temporary file where the external process places its result. That way, the process' direct output won't be big.


more remarks:

java.io.InputStream is=p.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);

I don't think that will reliably work, either. available won't necessarily contain all data, just indicate how much data there is before it blocks to get more.

Is it necessary to read the whole thing into memory?

System.out.println(new String(b));

Why turn the bytes into a String just to print them again? Cannot you just print the bytes directly?

Thilo
  • 257,207
  • 101
  • 511
  • 656