简体   繁体   English

使用C ++中的Java

[英]Using Java from C++

As a C++ developer, I occasionally come across Java libraries like iText , Batik , JasperReports , and JFreeChart . 作为一名C ++开发人员,我偶尔会遇到像iTextBatikJasperReportsJFreeChart这样的Java库。 In each case, equivalent cross-platform C++ libraries seem to be much less mature, much more expensive, or unavailable. 在每种情况下,等效的跨平台C ++库似乎都不太成熟,更昂贵或不可用。

Is it practical to use these Java libraries from my C++ app for reporting, charting, and similar? 使用我的C ++应用程序中的这些Java库进行报告,制图和类似工作是否切实可行? If so, what's the best approach to doing so? 如果是这样,这样做的最佳方法是什么?

  • Use JNI to embed a JVM within my application? 使用JNI在我的应用程序中嵌入JVM?
  • Use GCJ to compile the Java libraries to native code? 使用GCJ将Java库编译为本机代码?
  • 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? 放弃,因为从C ++调用Java库太难实用,而是投入我的努力去寻找C ++库?

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. 最简单的集成方法是老式的UNIX方法:启动一个小型Java程序,它可以完成您需要的任务并在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. 这在所有情况下都可能无法实现,但它绝对适用于PDF,SVG,报告和图表等用例,这些用例主要涉及生成单个文档以进行保存或显示。

Watch out for log4j, slf4j, JUL, etc. logging if you take this approach! 如果采用这种方法,请注意log4j,slf4j,JUL等日志记录! Anything that the Java program writes to standard out could corrupt the document you receive in the C++ program. Java程序写入标准输出的任何内容都可能破坏您在C ++程序中收到的文档。 Disabling logging or using sockets may be better in that case. 在这种情况下,禁用日志记录或使用套接字可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM