I need to find absolute path to javaw
in my C++ application.
javaw
can be run from command prompt & I can get its path using where javaw
but I need the path in C++
How can I find the path to javaw
in my C++ application?
Thanks
I need to find absolute path to javaw
in my C++ application.
javaw
can be run from command prompt & I can get its path using where javaw
but I need the path in C++
How can I find the path to javaw
in my C++ application?
Thanks
This code is literally copy-pasted from the top answer to How to execute a command and get output of command within C++? and then call from main
is added:
#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;
}
And this is what it prints on my Windows 7 machine:
C:\Windows\System32\javaw.exe
C:\Program Files (x86)\Java\jdk1.7.0_55\bin\javaw.exe
I guess you'd have to deal with the ambuiguities somehow, but I think I acheves why you're trying to do.
You could use a makro and compile with
-DJAVAW_PATH=`where javaw`