从 C++ 查找 JRE (视窗)
我正在为我的 Java 程序制作一个小型启动程序,其想法是我可以在调用 Java 之前设置内存要求、关联的库以及类似的东西。
然而,我越来越发现似乎没有一种固定的方法可以从任何东西调用 Java。
- system32 中不再有 java.exe,因此无法保证这一点。
- 在大多数计算机上,HKEY_LOCAL_MACHINE 或 HKEY_CURRENT_USER 下都有一个注册表项,它定义了 JRE 子项,然后定义了 JavaHome 值。这适用于大多数计算机,但在我的新计算机(全新的 java 安装,不到两周前构建)上,这些注册表不存在。 CURRENT_USER 中有一个 JRE 条目,但没有 JavaHome。
- JAVA_HOME 环境变量并不总是设置或设置正确。特别是在没有安装 JDK 的机器上。
- 我显然不能依赖将 Java 安装到 C:/Program Files/Java。
我应该如何能够从 C++ 程序可靠地调用 java 程序?或者就此而言,从任何地方?我可以让我的程序尝试所有四种可能的方法,但如果它们都是空的怎么办?
提前致谢。
I'm making a small launcher program for my Java program, the idea being that i can set memory requirements, associated libraries, and stuff like that before calling Java.
However, i'm increasingly finding that there doesn't seem to be a set-in-stone way to call Java from ANYTHING.
- It is no longer the case that there's a java.exe in system32, so that's not guaranteed.
- On most computers there's a registry entry either under HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER that defines a JRE subkey, then a JavaHome value. That works on most computers, but on my new computer (fresh java install, built less than two weeks ago) these registries don't exist. There's a JRE entry in CURRENT_USER, but no JavaHome.
- The JAVA_HOME environment variable isn't always set, or set correctly. Specifically on machines that don't have the JDK installed.
- I obviously can't rely on Java being installed to C:/Program Files/Java.
How the bleep am i supposed to be able to call a java program reliably from a C++ one? Or for that matter, from anywhere? I could have my program try all four possible methods, but what if they come up empty?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,强烈建议使用一种可用的脚本语言来查找 JRE 位置、设置内存要求以及类似的内容。由于 BAT 文件的语言相对较差,您可以使用 VBS 或 JScript 并使用名为
cscript
的实用程序运行它们。关于 JRE 所在的事实。
通常,JRE 可以在注册表中找到。例如,在我的机器上我可以看到以下条目。
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6.0_26
可能您有较新的安装...无论如何,请检查此处的 Java 安装条目:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
就我而言,我有条目
{26A24AE4-039D-4CA4-87B4-2F83216012FF}
,其中名为InstallLocation
的键包含 Java Home 的路径。下一个方法是检查 Java 主环境变量。
最后一种方法是在%ProgramFiles%中执行搜索
First, finding the JRE location, setting memory requirements and stuff like that is highly recommended to implement using one of available scripting languages. Due to the language of BAT files is relatively poor you can use VBS or JScript and run them using utility named
cscript
.Concerning to the fact where JRE is.
Typically JRE can be found in registry. For example on my machine I can see the following entry.
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.6.0_26
Probably you have newer installation... Anyway check entry for Java installation here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
In my case I have there entry
{26A24AE4-039D-4CA4-87B4-2F83216012FF}
where key namedInstallLocation
contains path to Java Home.The next way is to check Java home environment variable.
The last way is to perform search in %ProgramFiles%
在您的安装程序中尝试所有方法,也许您会发现多个 JRE,也可能没有。然后显示检测到的 JRE 并允许选择要使用的用户。用户还可以指向 JRE,特别是当安装程序找不到它时。
In your installer try all methods, maybe you find more than one JRE, maybe none. Then present detected JREs and allow selecting which user want to use. User can also point JRE, especially when installer did not find it.
Eclipse 使用 C shim (eclipse.exe) 生成 JVM。您可以查看该源代码并进行修改。
Eclipse uses a C shim (eclipse.exe) to spawn a JVM. You might to take a look at that source code and adapt.