简体   繁体   English

在C ++中获取java.home的位置

[英]Get location of java.home in C++

What would be the best way to get the location of java.home in my C++ app? 在我的C ++应用程序中获取java.home位置的最佳方法是什么? Right now I'm getting it from the registry but am wondering if there's a better/easier/cleaner way. 现在,我从注册表中获取它,但想知道是否有更好/更容易/更清洁的方法。

Thanks, Serge 谢谢,谢尔

if JAVA_HOME is set to the java home directory. 如果JAVA_HOME设置为java主目录。 If not val will be null. 如果不是val将为null。

char* val = getenv("JAVA_HOME" ); char* val = getenv("JAVA_HOME" );

If you're on a Unix/Linux box, perhaps you could do "which java". 如果您使用的是Unix / Linux,则可以执行“哪个Java”。 If you need assistance doing this in C++, see How to execute a command and get output of command within C++ using POSIX? 如果您需要在C ++中执行此操作的帮助,请参阅如何使用POSIX在C ++中执行命令并获取命令输出?

If you're on a Windows box, there is a "which.bat" someone made that I've used with success in the past, although it's not as good. 如果您使用的是Windows机器,那么我过去曾经成功使用过的“ that.bat”程序就可以成功使用,尽管效果并不理想。 You'd have to google for it. 你必须用谷歌搜索它。

Hopefully this can point you in the right direction! 希望这可以为您指明正确的方向!

There is often a copy of javaw in the System32 directory of a Windows installation, so getenv("windir") , and concatenate "\\System32\\javaw.exe" . Windows安装的System32目录中通常有一个javaw副本,因此getenv("windir")并连接"\\System32\\javaw.exe"

The second choice is then to open a File Browser dialog to choose the location of javaw.exe. 然后,第二个选择是打开“文件浏览器”对话框以选择javaw.exe的位置。

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

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