Hello.

I use Linux, and I have no experience in windows. I have to write a C++ program that checks if Java is installed. I have no idea how to check that (there is no directory such /usr/bin ).
I need this for a Java app and I want check utility. Can you give me a example how to check if something is installed in windows.

Thanks in advance,

Jan Birsa

Dani AI

Generated

Practical, reliable ways to detect a Java install on Windows (builds on 's registry note and 's batch idea). The registry is the canonical place installers record Java, but a Win32-only solution is not the only option. A simpler, portable strategy that avoids direct Win32 registry calls is: 1) check the JAVA_HOME environment variable, 2) scan PATH for a java executable, and 3) run that executable with -version to verify it’s real Java. If those fail, fall back to a registry query or spawn the reg command as suggested.

Example C++ sketch (portable, uses std::filesystem and a small helper to run java -version and capture output):

#include <cstdlib>
#include <string>
#include <sstream>
#include <filesystem>
#include <cstdio>

#ifdef _WIN32
#define PATH_SEP ';'
#define JAVA_BIN "java.exe"
#define popen _popen
#define pclose _pclose
#else
#define PATH_SEP ':'
#define JAVA_BIN "java"
#endif

// find java via JAVA_HOME or PATH (returns full path or empty)
std::string findJava() {
  if (auto jh = std::getenv("JAVA_HOME")) {
    auto p = std::filesystem::path(jh) / "bin" / JAVA_BIN;
    if (std::filesystem::exists(p)) return p.string();
  }
  if (auto p = std::getenv("PATH")) {
    std::istringstream ss(p); std::string part;
    while (std::getline(ss, part, PATH_SEP)) {
      auto candidate = std::filesystem::path(part) / JAVA_BIN;
      if (std::filesystem::exists(candidate)) return candidate.string();
    }
  }
  return {};
}

// run "<path> -version 2>&1" and look for "version" / "openjdk"
std::string capture(const std::string &cmd) {
  std::string out; FILE* f = popen(cmd.c_str(), "r"); if (!f) return out;
  char buf[256]; while (fgets(buf, sizeof(buf), f)) out += buf;
  pclose(f); return out;
}

Notes and caveats: java -version often writes to stderr, so redirect 2>&1 when capturing. JAVA_HOME isn’t mandatory and PATH entries can differ between system/user contexts. On Windows x64 watch for 32-bit vs 64-bit locations (some installs appear under Wow6432Node); reading those keys requires either the Win32 registry API or invoking reg query. UAC or restricted accounts may prevent registry reads—hence the recommended env/PATH/exec-first approach for the least-privileged, most portable check.

Recommended Answers

All 4 Replies

Look into the functions RegOpenKeyEx, RegQueryValueEx and RegCloseKey. They are used for opening the registry, reading values from the registry and closing it. Refer the MSDN for more details.

An example snippet here.

is there any less complicated way to check this?

Is possible to do this without use of win32 API?

No, for both.

If you need a less complicated way, the only thing you can do is follow the code example and make one for your needs.

And since Registry is windows specific, you need to use W32 API to access the registry. There is no portable or easy way out, as far as my knowledge goes.

Hello.

I use Linux, and I have no experience in windows. I have to write a C++ program that checks if Java is installed. I have no idea how to check that (there is no directory such /usr/bin ).
I need this for a Java app and I want check utility. Can you give me a example how to check if something is installed in windows.

Thanks in advance,

Jan Birsa

I just want to confirm that it is absolutely necessary for you to write a C++ program running in Windows to determine if Java is installed. If it isn't, you could just write a batch file program that uses the "REG QUERY" command to query the registry, and then you'd check the "%ERRORLEVEL%" Global variable to see if the program returned sucessful (which means the key does exist) or not (which means it doesn't). If you weren't aware already, a batch file is similar to a bash script.

Introduction to Batch file programming for Windows:

For more information regarding the "REG" command, open up a DOS prompt type either of these:

reg /? - this will show you all of the possible reg functions
reg query /? - this will show you how to query the registry


However, if it is necessary that you use C++ then my suggestion is the same as ~S.O.S~'s.

Good luck, LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.