Solved

Determine JVM version using C++

Posted on 2004-08-07
4
240 Views
Last Modified: 2010-04-01
Hi,

How do I determine the version number of the currently running JVM using C++ on a Window OS machine?

I have considered accessing jview (either as a dll or as a ShellExecute command to get the version number of the file), but I'm curious to see if there are better solutions.

Any comments, advice or suggestions will be handsomely rewarded. :)

TIA,
S/.
0
Comment
Question by:stitch2802
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 11744004
Spawning "jview.exe /?" gives the version number of the installed JVM. This info can also be obtained using

#include <windows.h>
#include <iostream>
using namespace std;

#pragma comment(lib,"version.lib")

   void reportError()
   {
       LPVOID lpMsgBuf;

       FormatMessage(
           FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
           NULL,
           GetLastError(),
           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
           (LPTSTR) &lpMsgBuf,
           0,
           NULL );

       cout << (char*)lpMsgBuf << "\n";

       // Free the buffer.
       LocalFree( lpMsgBuf );
   }

   void main( int argc, char *argv[ ], char *envp[ ] )
   {
       cout << "\n";

       if(2 != argc)
       {
           cout << "Syntax: GetVer <File Name>\n";
           return;
       }

       DWORD dwArg;
       DWORD dwInfoSize = GetFileVersionInfoSize(argv[1], &dwArg);

       if(0 == dwInfoSize)
       {
           cout << "No version info available\n";
           reportError();
           return;
       }

       BYTE* lpBuff = new BYTE[dwInfoSize];

       if(!lpBuff)
       {
           cout << "Out of Memory\n";
           return;
       }

       if(0 == GetFileVersionInfo(argv[1], 0, dwInfoSize, lpBuff))
       {
           cout << "Error retrieving version info\n";
           reportError();
           return;
       }

       VS_FIXEDFILEINFO *vInfo;

       UINT uInfoSize;

       if(0 == VerQueryValue(lpBuff, TEXT("\\"),
               (LPVOID*)&vInfo,
               &uInfoSize))
       {
           cout << "Version information not available\n";
           delete lpBuff;
           return;
       }

       if(0 == uInfoSize)
       {
           cout << "Version information not available\n";
           delete lpBuff;
           return;
       }

       cout << argv[1]
           << " Version: "
           << HIWORD(vInfo->dwFileVersionMS) << ","
           << LOWORD(vInfo->dwFileVersionMS) << ","
           << HIWORD(vInfo->dwFileVersionLS) << ","
           << LOWORD(vInfo->dwFileVersionLS) << "\n";

       delete lpBuff;
   }

(original code taken from http://support.microsoft.com/default.aspx?scid=kb;en-us;167597)
0
 

Author Comment

by:stitch2802
ID: 11744925
Hi jkr,

Thanks for answering, but supppose the user has both JVM versions on his/her computer - as in both the Microsoft JVM and the Java VM - won't jview just give me information about the Microsoft JVM and none whatsoever for the Java VM?

I'm trying to figure which version is running as well.

Any advice? :)

TIA,
S/.

0
 
LVL 86

Expert Comment

by:jkr
ID: 11744994
Well, then just grab the version info from all files in queston :o)

(I'm not too familiar with Java and it's VMs, but obtaining the version information is the same for all)
0
 

Author Comment

by:stitch2802
ID: 11744996
Ok, thanks jkr! =)

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now