如何调用 c++ 来自java的功能

发布于 2024-07-29 23:00:46 字数 245 浏览 6 评论 0原文

我有一个主要是 GUI 的 Java 程序,它显示从 C++ 命令行工具写入 xml 文件的数据。 现在我想在java程序中添加一个按钮来刷新数据。 这意味着我的程序必须调用 C++ 功能。

通过系统调用从 java 调用程序的最佳方法是? c++ 程序将为 mac os 和 windows 编译,并且应始终与 java 程序位于同一目录中。

我想生成一个可执行文件,c 程序可以存储在 jar 中并从我的程序中调用吗?

I have a Java program that is mostly GUI and it shows data that is written to an xml file from a c++ command line tool. Now I want to add a button to the java program to refresh the data. This means that my program has to call the c++ functionality.

Is the best way to just call the program from java through a system call?
The c++ program will be compiled for mac os and windows and should always be in the same directory as the java program.

I would like to generate an executable can the c program be stored inside the jar and called from my program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧城空念 2024-08-05 23:00:47

要回答您的最后一个问题,您无法从 jar 中运行可执行文件。

但是,您可以将其存储在 jar 中,并在运行之前将其解压缩到临时目录/文件(第一次检查其存在,并在必要时解压缩)。 这将在一定程度上简化您的分发,因为您只需分发 jar,并确保您正在运行与 jarred Java 代码相匹配的可执行文件。

To answer your final question, you can't run an executable from within your jar.

However, you can store it within your jar and extract it to a temporary directory/file prior to running it (check for its presence the first time and extract if necessary). This will simplify your distribution somewhat, in that you only have the jar to distribute, and ensures that you're running an executable that matches your jarred Java code.

提笔落墨 2024-08-05 23:00:47

如果您有权访问代码并希望获得与外部程序的“交互”体验(例如,进行调用、获取结果、进行其他调用),请研究 JNI,它允许您通过包含以下内容从 Java 应用程序调用 C 或 C++ 代码& 使用 .

请参阅:

http://en.wikipedia.org/wiki/Java_Native_Interface

http://www.acm.org/crossroads/xrds4-2/jni.html

如果您实际上只需要一个“启动应用程序并获取结果”之类的解决方案,请查看 Runtime.exec(),它可以让您启动外部程序并获取结果。 捕获其输出。

看:
http://www.javaworld.com /javaworld/jw-12-2000/jw-1229-traps.html?page=1

http://www.rgagnon.com/javadetails/java-0014.html

If you have access to the code and want an 'interactive' experience with the external program (e.g., make call, get results, make additional calls), investigate JNI, which allows you to call C or C++ code from a Java application by including & linking JNI juice to your C or C++ app with .

See:

http://en.wikipedia.org/wiki/Java_Native_Interface

http://www.acm.org/crossroads/xrds4-2/jni.html

If you really just need a "launch app and get results" sort of solution, check out Runtime.exec(), which lets you launch an external program & capture its output.

See:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

http://www.rgagnon.com/javadetails/java-0014.html

逆夏时光 2024-08-05 23:00:47

您可能还想查看Java Native Access API (JNA)

You may also want to look at the Java Native Access API (JNA).

聊慰 2024-08-05 23:00:47

假设没有更好的通信方法可用(SOAP、ICE、套接字等),我将使用 Runtime.exec() 调用可执行文件。 JNI可以直接用于接口,但我不推荐它。 不,你不能将可执行文件放入 jar 中。 好吧,你可以,但你不能运行它,因为 shell 不知道如何运行它。

Assuming no better communication method is available (SOAP, ICE, Sockets, etc), I'd call the executable using Runtime.exec(). JNI can be used to interface directly, but I wouldn't recommended it. No you can't put an executable in the jar. Well you can, but you can't run it, since the shell doesn't know how to run it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文