Search code examples
javawindowscommand-promptjscriptwsh

How to collect output from running Java applet via JScript in Windows command prompt?


I am trying to automate compression of multiple JS/CSS files with the YUI Compressor. Each time I basically need to run the following Java command:

java -jar yuicompressor-2.4.8.jar "script in.js" -o "script out.js"

So I do it using Microsoft's JScript engine:

var oShell = WScript.CreateObject("WScript.Shell");

var scriptIn = "script in.js";
var scriptOut = "script out.js";

var strRun = "java -jar yuicompressor-2.4.8.jar \"" + scriptIn + "\" -o \"" + scriptOut + "\"";

oShell.Run(strRun, 0, true);

This works fine, except that it doesn't give me any output from the compressor. Note that if I put that same line of Java code in a .bat file and run it from a command line, it gives me the output from running the command (YUI Compressor's warnings, errors, etc.)

So I'm curious, how do I collect it via my JScript?


Solution

  • Use .Exec instead of .Run and capture the objects StdErr:

    var exec = oShell.Exec(strRun);
    WScript.Echo(exec.StdErr.ReadAll());
    

    (specify --verbose in strRun; use the docs to replace the hack above with a more robust version that checks .Status and .StdErr.AtEndOfStream)