views:

90

answers:

2

As a C++ developer, I occasionally come across Java libraries like iText, Batik, JasperReports, and JFreeChart. In each case, equivalent cross-platform C++ libraries seem to be much less mature, much more expensive, or unavailable.

Is it practical to use these Java libraries from my C++ app for reporting, charting, and similar? If so, what's the best approach to doing so?

  • Use JNI to embed a JVM within my application?
  • Use GCJ to compile the Java libraries to native code?
  • Some other integration method that I'm not aware of?
  • Give up, since calling a Java library from C++ would be too hard to be practical, and instead invest my efforts in finding C++ libraries?
+3  A: 

The least complicated method of integration is the old-school UNIX approach: launch a small Java program that does the task you need and communicate with it on STDIN/STDOUT.

This may not be possible in all cases, but it definitely is for use cases like PDF, SVG, reporting and charting which largely involve generating single documents for saving or display.

Watch out for log4j, slf4j, JUL, etc. logging if you take this approach! Anything that the Java program writes to standard out could corrupt the document you receive in the C++ program. Disabling logging or using sockets may be better in that case.

Alain O'Dea
A: 

.. I was just about trying to explain why it would not be possible, when I came across this article: How to call Java functions from C using JNI. Sounds like a solution.

Andreas_D