How to use Wscript Exec to run a Java file

2019-09-11 05:50发布

问题:

As the description of Wscript:String value indicating the command line used to run the script: The command line should appear exactly as it would if you typed it at the command prompt.

I can run my java file using the command "java test http://www.bbc.co.uk/news/world-us-canada-12116778" but it is not working when I wrote the JavaScript below. Can someone can tell me why? Thank you or can tell me there is some other method to call my Java file when I open a html file?

<script type="text/javascript">
funciton {}
var WshShell = new ActiveXObject("WScript.Shell");
var oExec= WshShell.Exec(""java test http://www.bbc.co.uk/news/world-us-canada-12116778"");
while (oExec.Status == 0)
{
     WScript.Sleep(100);
}
</script>

回答1:

Take a look at the WSHSell object's run method. The following code works for me:

var shell = new ActiveXObject("WScript.Shell");
shell.run("cmd /c java -jar MyApplication.jar");
// should work without JARs as well, take care for the working path

The run method has an option to wait for the java program to return.

Hope this helps.