在 c++ 中执行 jar 文件程序
我正在开发一款 CAD 软件,其中对于我的图形部分,我使用opnegl & 内核正在用c++开发。 对于窗口界面,我被建议使用QT,但由于我的软件是用于商业用途,我不想使用QT,而是使用Java。 我的问题是,我可以在我的 C++ 程序中使用 jar exe (因为我的内核是 C++ 且内核控制该程序)? 如果是的话,任何人都可以提供一些简单的例子或一些网站。 如果没有,其他选择是什么?
我需要 Windows 界面主要是为了让用户选择他想要执行的操作(创建点、线、圆等),通过提供一些小图标,用户可以点击< /em>。 我还需要弹出窗口的 Windows 界面来显示警告、错误、接受输入参数等。
I am developing a CAD software in which for my graphics part i am using opnegl & the kernel is being developed in c++.
For the window interface i was adviced to use QT, but since my software is for commercial use i do not want to use QT, but rather use Java.
My problem is, can i use jar exe in my c++ program (since my kernel is in C++ & the kernel controls the program)?
If yes can anybody provide some simple example or some site.
If no what is the other option?
I require the windows interface mainly for letting the user select the operations he wants to perform (create point, line, circle, etc), by providing some small icons on which the user can click.
I also require the windows interface for pop-up window to show warnings, errors, take input arguments, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不加修改地使用 Qt 的 dll,您甚至可以在商业项目中使用 Qt。
但您也可以使用 JNI(Java 本机接口)将 java 与 c++ 连接。或者你可以使用类似 swig 的东西。 Swig 从 C++ 类生成 Java 类。它大大简化了两种语言接口的工作。
您需要用 Java 启动应用程序,这可能是必要的,但与您想要的不同。然后Java通过dll加载c++内核,然后调用函数。
You could use Qt even in comercial projects if you use their dll's without modification.
But you also could use JNI (Java native interface) to interface java with c++. Or you could use something like swig. Swig generates java classes from c++ clases. It simplifies the work of interfacing the two languages a lot.
It is maybe neccessary and different from what you wanted, that you need to start your application in Java. The Java then loads the c++ kernel via a dll and then calls the functions.
您可以将 Java 虚拟机直接嵌入到 C++ 程序中。 JVM 提供了一个 API,允许在与本机程序相同的进程空间中实例化 VM。
然而,我认为与其将 JVM 嵌入到您的本机应用程序中,不如采取相反的做法:让 JVM 调用您的本机代码。
两者都需要使用 Java 本机接口 (JNI),但在第一种情况下,需要针对向 VM 公开的本机功能执行一些额外的步骤。这些额外的要求可能很难满足大规模本机应用程序。
请参阅 Java 本机接口手册中的 第 8.3 章:
8.3 注册本机方法
[...]
RegisterNatives当本机应用程序嵌入虚拟机实现并需要与本机应用程序中定义的本机方法实现链接时,RegisterNatives 特别有用。虚拟机将无法自动找到此本机方法实现,因为它只在本机库中搜索,而不是应用程序本身。
换句话说,所有非 dll_exported 本机方法都必须手动注册到虚拟机。这是一个相当重量级的要求。
You can embed the Java Virtual Machine directly into a C++ program. The JVM provides an API that allow to instantiate a VM in the same process space as your native program.
However, instead of embedding a JVM into your native application, I think it is a better idea to do the reverse: let the JVM call your native code.
Both require usage of the Java Native Interface (JNI), but in the first case there is a few additional steps to be done regarding native functions exposed to the VM. These additional requirements may be difficult to satisfy with large-scale native applications.
See the chapter 8.3 from the Java Native Interface Manual:
8.3 Registering Native Methods
[...]
RegisterNatives is particularly useful when a native application embeds a virtual machine implementation and needs to link with a native method implementation defined in the native application. The virtual machine would not be able to find this native method implementation automatically because it only searches in native libraries, not the application itself.
Put it differently, all non dll_exported native methods must be manually registered to the VM. This is quite a heavyweight requirement.
我不知道如何通过 C++ 运行 JAR 文件(除了明显的
system()
或CreateProcess()
或fork()\exec( )
调用以从操作系统本身的 C++ 中转义)。也就是说,您可能会考虑让您的应用程序由多个通过 TCP 或 UDP 套接字进行通信的独立进程组成。然后每篇文章都可以用任何最有意义的语言来编写。例如,您可以让应用程序的中央核心(用 C++ 编写)设置一个 TCP 服务器,然后让“工具栏”应用程序(用 Java 编写)通过专用套接字连接将命令数据包发送到核心程序。想想看,UDP 可能更适合这一点。无论如何,你明白了。
另外,不确定你拒绝 Qt 的原因——很多商业应用程序都使用了它,而且它是相当可定制的(尽管我必须承认,由于其他原因,我自己并不是它的忠实粉丝)。
I don't know exactly how to run a JAR file via C++ (other then the obvious
system()
orCreateProcess()
orfork()\exec()
calls to escape from C++ proper to the OS).That said, you might consider having your application consist of multiple, independent processes that communicate via TCP or UDP sockets. Then each piece could be written in whatever language makes the most sense. For example, you could have the central core of the application (written in C++) set up a TCP server, then have "toolbar" apps (written in Java) send command packets to the core program via a dedicated socket connection. Come to think of it, UDP is probably better for that. Anyways, you get the point.
Also, not sure about your reasons for rejecting Qt--plenty of commercial apps have used it, and it is quite customizable (though I must confess I'm not a big fan of it myself, for other reasons).
无论你做什么,都不要使用 system()。 //原因如下: http://www.cplusplus.com/forum/articles/11153/< /a>
Whatever you do, do NOT use system(). //Here's why: http://www.cplusplus.com/forum/articles/11153/