How to run .js file from a command line on windows?

You have two options:

Cscript.exe "C:\Users\Public\TestPro\TestPro Automation Framework\taf.js"

or

Wscript.exe "C:\Users\Public\TestPro\TestPro Automation Framework\taf.js"

The former one starts the command line version of Windows Scripting Host and the latter one is starting the window version.

The command line options are documented here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cscript

and here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/wscript

In your script file you should use the full path to your jar file and possibly full path to the java.exe

Alternatively, to allow the java.exe locate the tar-loader.jar file you should change the current directory to the one containing this file, so before invoking wscript make cd "C:\Users\Public\TestPro\TestPro Automation Framework\"

As you mentioned that you like to run this script in a single touch, you also may create a shortcut on your desktop that has a command line set to Wscript.exe "C:\Users\Public\TestPro\TestPro Automation Framework\taf.js" and working directory set to C:\Users\Public\TestPro\TestPro Automation Framework\


If you can run 'java' in a console window with no errors, then

1) Create a text file on your desktop called 'taf.txt'

2) Enter "java -Dlog.dir=%TEMP% -jar taf-loader.jar" into the text file, and save it

3) Rename the file to 'taf.bat'

Done.

Using a scripting host to start a script which launches a shell that launches a java program is just too long winded.

Think of it in terms of what you want to achieve - you want the program to launch with a click of an icon.

The batch file is the fastest way to achieve that on win7.

If you were using an OS that could natively kick off a Javascript script with no messing about it would be a nice way to launch it, but that script wouldn't work on anything other than windows (unless ActiveX is ported now - been a long time since I've used it).

Source: Professional Java Developer for over 10 years