I'm trying to retrieve the java.exe
installation path on Windows. However, I can't succeed so far.
What I have tried are:
getenv("JAVA_HOME")
, which is rarely set in my opinion (mostly returns nothing, since it isn't set?)- CMD the command
for %i in (java.exe) do @echo. %~$PATH:i
, which returns me the path when executed manually in cmd, however I can't retrieve it programmatically in c++?_popen
returns me nothing when I execute this code, however a simpledir
statement does return me the right output (directory listing).
So, all I want is to retrieve the path where java.exe
is located, which I need in my application. Please if you have any good ideas, let me know. I'm willing to fix this issue.
string cmd(char *command) {
FILE *fpipe;
string response = "";
char c = 0;
if (0 == (fpipe = (FILE*)_popen(command, "r"))) {
Exit("popen() failed.");
}
while (fread(&c, sizeof c, 1, fpipe)) {
//printf("%c", c);
response += c;
}
_pclose(fpipe);
return response;
}
cout << cmd("for %i in (java.exe) do @echo. %~$PATH:i") << endl; //EMPTY (MANUAL ON CMD RETURNS THE RIGHT PATH)
cout << cmd("dir"); //RETURNS LIST OF CONTENTS