0

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

Ariyan
  • 14,760
  • 31
  • 112
  • 175

2 Answers2

1

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.

Community
  • 1
  • 1
Alexander Balabin
  • 2,055
  • 11
  • 13
  • Although using `system` is very heavy ,I accept the answer because this works too. At the end I iterated through `PATH` environment variable & checked to find `java.exe` in each path; – Ariyan Jul 07 '15 at 18:39
0

You could use a makro and compile with

-DJAVAW_PATH=`where javaw`
JoshOvi
  • 88
  • 7