cancel
Showing results for 
Search instead for 
Did you mean: 

How to get installed Java (JRE) version details and post it to the log?

mgroen
Frequent Contributor

How to get installed Java (JRE) version details and post it to the log?

Our application uses Java (JRE) to run.



I want to get the installed Java details (version is most important), and post it to the log of running TestComplete tests.

So that I can see which Java is used in the test execution.



What would be the best approach? I mainly use keyword testing but when needed I use Code Snippets / small pieces of scripts.



Thanks.

Mathijs
11 REPLIES 11
jose_pita
Super Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

2 options:


  1. using testcomplete, run the cmd command "java -version" and copy the text from the command line


  2. use a bat file to run the same command and using TC, just copy the result


If you need help doing this feel free to shout 😄
AlexKaras
Community Hero

RE: How to get installed Java (JRE) version details and post it to the log?

Hi,



Good question that I might like to know the answer to as well... (like the similair question about what .Net runtime(s) is/are used for the given .Net application.)



Jose, thank you for the provided command-line. The problem, according to my understanding, is that it will return the version of the first found Java executable.

For example, when I executed java -version just from the command line, the returned version was (build 1.7.0_21-b11).

However, when I executed the same command from the jdk1.6.0_45\bin folder, the result was (build 1.6.0_45-b06).

And when I executed this command from the LoadUI-2.6.8\jre\bin folder, the result was (build 1.7.0_15-b03).

It is my guess that when I start LoadUI it is executed using either (build 1.7.0_15-b03) Java version (because it is in its subfolder and LoadUI might rely on this) or (build 1.7.0_21-b11) Java version (because this version can be found via some environment variable). Some other application might be directly pointed to use the (build 1.6.0_45-b06) Java version.



So, I think that the real question was like "how to determine what Java version is used to run this given Java application instance".

Wondering if anyone have searched for the answer...
Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
jose_pita
Super Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

It's simple if you know where to look.



Using a batch you can do whatever commands you want including navigating to a folder of your choice and perform the "java -version" there.



I use this to run commands using the windows task scheduler, otherwise, all the commands are run in "C:\Windows\System32", I must navigate to the folder I need to perform the command.



Again, if you need the code to do this, feel free to ask.
mgroen
Frequent Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

Alexei,

very good addendum. That's exactly what I am looking for.

Thanks for the suggestion Jose Pita, however I would prefer not to invoke cmd screens, copy/paste etc,
More cleaner approach would be something similar in browsing version approach:

browser.description

resulting in a string of the current used browser name and version.

Would be great if such a similar approach could be used for current Java JRE instance.

Anybody got an idea?


jose_pita
Super Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

I still haven't figured out how to execute the command in TC (maybe alexey can help you with that), but here it how I do it:


  1. add the bat file with the command to TC testedApps


  2. run the bat file from within TC


  3. use this to capture the text:


var proc = Sys.FindChild("ProcessName", "cmd");  // Obtains the process by its name


Log.Message(aqString.SubString(proc.child(0).wText,122,80))//adjut the indexes acording to your need

mgroen
Frequent Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

Jose, what should be in the bat command?
AlexKaras
Community Hero

RE: How to get installed Java (JRE) version details and post it to the log?

To execute some command and get back its output you may try one of two functions below (readTillCharTest(); or ExecAndWaitForEnd()) published long time ago on TC forum (they are in DelphiScript, but I hope it will not be a problem to convert them to JScript):



//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------


function readTillChar(var WshShellExecObj : OleVariant; endChar : string) : string;

  var strOut : string;

  var curChar : string;

begin

   strOut := '';

   while (not WshShellExecObj.StdOut.AtEndOfStream) do

   begin

     curChar := WshShellExecObj.StdOut.Read(1);

     strOut := strOut + curChar;

     if (curChar = endChar) then break;

   end;

   result := strOut;

end;

//-----------------------------------------------------------------------------



function readTillCharTest();

  var WshShellObj : OleVariant;

  var WshShellExecObj : OleVariant;

  var strOut : string;

begin

   WshShellObj := Sys.OleObject('WScript.Shell');

   WshShellExecObj := WshShellObj.Exec('cmd.exe');



   // flush the stream

   strOut := readTillChar(WshShellExecObj, '>');

   Log.Message(strOut);



   // send the 'ver' command and the new line character

   WshShellExecObj.StdIn.Write('ver' + #13#10); // 'ver\n' for JScript

   strOut := readTillChar(WshShellExecObj, '>');

   Log.Message(strOut);

end;

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------



function ReadAllFromAny(oExec : OleVariant) : string;

begin

  result := '';

  if (not oExec.StdOut.AtEndOfStream) then

  begin

    result := oExec.StdOut.ReadAll;

    exit;

  end;



  if (not oExec.StdErr.AtEndOfStream) then

  begin

    result := 'STDERR: ' + oExec.StdErr.ReadAll;

    exit;

  end;

end;

//-----------------------------------------------------------------------------



function ExecAndWaitForEnd(strProg, strArgs : string; var strOutput : string) : integer;

  const cEXEC_DELAY = 500; // time interval (in ms) when looking if spawned process have finished



  var WshShell : OleVariant;

  var oExec : OleVariant;

  var strCommand  : string;

  var allInput  : string;

begin

  result := -1;



  strProg := aqString.Trim(strProg);

  If ('"' <> aqString.GetChar(strProg, 0)) then

    strProg := aqString.Quote(strProg);



  strCommand := strProg + ' ' + strArgs;

  Log.AppendFolder('Executing: ' + strCommand, strCommand);

  try

    WshShell := Sys.OleObject('WScript.Shell');

    oExec := WshShell.Exec(strCommand);



    while (0 = oExec.Status) do

      BuiltIn.Delay(cEXEC_DELAY);



    allInput := ReadAllFromAny(oExec);



    result := oExec.ExitCode;

    strOutput := allInput;

    Log.Message(aqString.Format('Done. Exit code = %i (0x%x)', oExec.ExitCode, oExec.ExitCode), allInput);

  finally

    Log.PopLogFolder();



    oExec := nil;

    WshShell := nil;

  end;

end;

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------



I used to use ExecAndWaitForEnd() one, but it uses a reference parameter that is not allowed in JScript (though http://support.smartbear.com/viewarticle/42197/ piece of advise might help).

With DelphiScript I would write something like this:

var iRetCode : integer

var strOutput : string;

iRetCode := ExecAndWaitForEnd('cmd "-c java -version" ', '', strOutput);

if (0 = iRetCode) then

  Log.Message('Java version: ' + strOutput);

else

  Log.Error(...);



However, all this is good when you have control over the started Java application (i.e. you know where you've started it from and thus may consider what Java runtime will be used to execute it).

But what if, for example, the started Java application is some .jnlp component started after clicking a link in the browser? How can I consider in this case where this Java application was started from and what Java runtime was used to execute it?



(One idea that came to me is:

-- Get the list of running Java processes before starting the tested Java application;

-- Start tested Java application;

-- Compare the previous list with the list of Java processes running now and find Java process that appeared. Assume that this process is executing the tested Java application);

-- Using WMI or any other means find where the Java process was started from and get the version of java from the found location.



This should work... But require a bit of efforts for implementation:) )


Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
tonydugay
Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

This might work?  I am not 100% sure - see the screen shot of the NameMapping - see the FileVersionInfo of the java process.



It works for me in my situation - I have used NameMapping to recognise a Java Web Start application and I can Log the FileVersionInfo to the test log - hence logging the version of Java being used by that application.





jose_pita
Super Contributor

RE: How to get installed Java (JRE) version details and post it to the log?

getJavaVersion.bat :




cd C:\myFolder\


java -version

pause



after running the bat on TC, use this function:




function assddasd()


{


  var proc = Sys.FindChild("ProcessName", "cmd");  // Obtains the process by its name


  Log.Message(aqString.SubString(proc.child(0).wText,122,80)) //set the indexes as you need


}




New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors