6

I want to execute a java program from a javascript and want to get the output.

Intailly i tried with below code:

WshShell = new ActiveXObject("WScript.Shell");
var launch="cmd.exe /c java -classpath . HelloWorld ";
var cmdRun = WshShell.Run(launch,0,true);

Through Run method i am not able get the output of the class.

Then i tried with below code:

WshShell = new ActiveXObject("WScript.Shell");
var launch="cmd.exe /c p java classpath . HelloWorld ";
var cmdRun = WshShell.Exec(launch);
while (cmdRun.Status == 0) // wait for the command to finish
{
sleep(100);
}
var output = cmdRun.StdOut.ReadAll();
alert(output);

Now i am able to get the output in variable output.

My problem is using Run method i can hide the commandprompt(by passing parameters WshShell.Run(launch,0,true)) Where as by using Exec method i am not able to hide the commandprompt. I want this commandprompt to be hidden.

Can you please help me in this regard? Thanks

user2118354
  • 63
  • 1
  • 1
  • 3

2 Answers2

10

Yes, that bother all wsh scripters. No way to hide wshExec object, only .Run allow this option, but no StdOut in this case. Shortly, the only way is to redirect your output to file.

WshShell   = new ActiveXObject("WScript.Shell");
var launch ="cmd.exe /c java -classpath . HelloWorld > output.txt";
var cmdRun = WshShell.Run(launch,0,true);
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28
  • Eh, I just forgot to say welcome to StackOverflow ;-) And by the way, then you get valid answer mark it as accepted, that will pump points to both sides, me and you in this case, but that also will helps the rest readers of the forum to know that the question is answered ;-) – Panayot Karabakalov Feb 28 '13 at 11:02
  • Thank you Panayot!! I did as you suggested. – user2118354 Feb 28 '13 at 12:43
0

I know this thread is quite old, but I just came up against the same problem and found a solution by adding "exit" to the command string, which closes the window.

From the cmd page of Microsoft Docs:
You can specify multiple commands for [string]. Separate them by the command separator && and enclose them in quotation marks. For example:

"[command1]&&[command2]&&[command3]"

So the full command string you pass to WshShell.Exec would look like this:

        cmd.exe /c "java -classpath . HelloWorld&&exit"
SweeP
  • 1
  • 1