Forum Discussion
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
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:) )