问题
I want to find the version number of the current version of Java installed on the computer and the current version of Flash installed on any given web browser that has flash. Is there a way to do this using Java, C, or C++? If so, what class/library should I look into?
回答1:
If you are in Java
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vendor"));
System.out.println(System.getProperty("java.vm.name"));
output
1.7.0_03
Oracle Corporation
Java HotSpot(TM) Client VM
there are more props about Java
for(Map.Entry e : System.getProperties().entrySet()) {
if (((String)e.getKey()).startsWith("java")) {
System.out.println(e);
}
}
.
java.runtime.name=Java(TM) SE Runtime Environment
java.vm.version=22.1-b02
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
java.vm.name=Java HotSpot(TM) Client VM
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.7.0_03-b05
....
As for other languages I think you can do it by running %JAVA_HOME%/java -version from your app, reading the output
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
Or you can right a JavaProps app like the above, run it as %JAVA_HOME%/java JavaProps and read the output
回答2:
Detecting java version from C/C++ could be tricky, the thing is that there is not standard API you can use to get the installed JRE properties. A way to do it is creating a subprocess that invokes java -version and get the output from this process (also you can make the subprocess write to a specific file and then retrieve the data from it) , then parse it and get the version.
The following is a modified version of a Windows .exe launcher routine I did sometime ago that uses what I just explained (create the subprocess for java -version, make the subprocess write to a temp file, open the file and read its content, parse the version):
void printJVMVersion() {
STARTUPINFO si;
PROCESS_INFORMATION pi;
size_t sizeOfStartupInfo = sizeof(STARTUPINFO);
ZeroMemory(&si, sizeOfStartupInfo);
si.cb = sizeOfStartupInfo;
SECURITY_ATTRIBUTES sa = { sizeof(sa) }; // Open files in inheritable mode
sa.bInheritHandle = TRUE; // Allow inheritance
sa.lpSecurityDescriptor = NULL; // Handles to the child process
si.dwFlags = STARTF_USESTDHANDLES;
const std::string DEFAULT_TEMP_FILE_NAME = "temp_file.txt";
const size_t MAX_PATH = 256;
char buffer[MAX_PATH];
char tempPathBuffer[MAX_PATH];
GetTempPath(MAX_PATH, tempPathBuffer);
std::string file_name;
if ( GetTempFileName(tempPathBuffer, "some_random_prefix", 0, buffer) != 0 ) {
file_name = std::string(buffer);
} else {
file_name = DEFAULT_TEMP_FILE_NAME;
}
si.hStdError = CreateFileA(file_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, 0, NULL);
si.hStdOutput = CreateFileA(file_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, 0, NULL);
if (si.hStdOutput == NULL) {
MessageBox(NULL, "Error checking for the installed Java Virtual Machine, operation aborted", "Launching", MB_ICONERROR | MB_OK);
return false;
}
if (!CreateProcessA(NULL, "java -version", NULL, NULL, true, 0, NULL, NULL, &si, &pi)) {
MessageBox(NULL, "It appears that neither you have the Java Virtual Machine installed on your system nor its properly configured", "Launching", MB_ICONERROR | MB_OK);
ErrorExit("CreateProcessA");
return false;
}
WaitForSingleObject( pi.hProcess, INFINITE );
if( si.hStdError ) {
CloseHandle( si.hStdError );
}
if( si.hStdOutput ) {
CloseHandle( si.hStdOutput );
}
// "Parse" the txt file
std::ifstream ifs(file_name.c_str());
std::string line;
int versionIndex = 0;
int value[2];
value[0] = value[1] = 0;
while (std::getline(ifs, line)) {
const std::string JAVA_VERSION_STRING = "java version ";
size_t index = line.find(JAVA_VERSION_STRING.c_str());
if (index != std::string::npos) {
// get either the 1.X.X or 2.X.X
std::string version = line.substr(JAVA_VERSION_STRING.size());
std::string::iterator ite = version.begin();
std::string::iterator end = version.end();
std::string tmp = "";
for (; ite != end; ++ite) {
char c = *ite;
if (isdigit(c)) {
tmp += c;
} else if (c == '.') {
value[versionIndex] = atoi(tmp.c_str());
versionIndex++;
tmp = "";
// If we have, version, major and minor, then another set of digits is unnecessary
if (versionIndex == 2)
break;
}
}
std::cout << "detected java version: " << (value[0]) << ", " << (value[1]) << std::endl;
}
}
// Delete the temp file
DeleteFile(file_name.c_str());
}
Hope this can be of any help
来源:https://stackoverflow.com/questions/13851750/programmatically-getting-version-number-of-java-and-flash-using-c-c-or-java