我需要在我的 C++ 应用程序中找到 javaw
的绝对路径。
javaw
可以从命令提示符运行,我可以使用 where javaw
获取它的路径,但我需要 C++ 中的路径
如何在我的 C++ 应用程序中找到 javaw
的路径?
谢谢
最佳答案
此代码实际上是从对 How to execute a command and get output of command within C++? 的最佳答案复制粘贴而来的然后添加来自 main
的调用:
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(char* cmd) {
FILE* pipe = _popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
_pclose(pipe);
return result;
}
int main()
{
std::cout << exec("where javaw") << std::endl;
return 0;
}
这是它在我的 Windows 7 机器上打印的内容:
C:\Windows\System32\javaw.exe
C:\Program Files (x86)\Java\jdk1.7.0_55\bin\javaw.exe
我猜你必须以某种方式处理歧义,但我想我明白你为什么要这样做。
关于c++ - 查找可以从 CMD 运行的可执行文件的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262285/