How to Convert your javascript file to a Windows .exe or .dll

Nowadays an average web developer will need to know a broad matrix of skills in order to do his/her development job. HTML, CSS, JavaScript, AJAX, XML, some server side language, some linux/unix/redhat command skills, some apache, some database skills, standards, accessibility, SEO, etc…, the list goes on. Parts of the list are also multiplied by (or raised to the power of at least 10) number of browsers you want to support.
What is Javascript can be used for?

  • create rich and powerful web applications (the kind that runs inside the browser)
  • write server-side code such as ASP scripts or for example code that is run using Rhino (A JavaScript engine written in Java)
  • create rich media applications (Flash, Flex) using ActionScript which is based on ECMAScript, which is JavaScript
  • write scripts that automate administrative tasks on your Windows desktop, using Windows Scripting Host
  • write extensions/plugins for a plethora of desktop application such as Firefox or Dreamweaver
  • create web applications that store information off-line on user’s desktop, using Google Gears
  • create Yahoo!, or Mac, or dunno-what-type-of widgets
  • create Windows apps (those that end in .exe) and libraries (.dll)

Get the JS Compiler

You’ll find the jsc.exe (found one on mine in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727), then add this path to your environment path: Right-click My Computer – Advanced – Environment Variables – System Variables – Path – Edit

Now open command prompt (Start – Run – “cmd” – OK) and type “jsc”+ENTER. You should see a list of help options for the compiler.

First .exe

The last time I created an .exe file is probably years ago, when I was developing a desktop app for a Limosine Management Company in Visual Basic.

Create a file called test.js in C:\\myapps directory with the following content:

var d = new Date();
var n = Math.random();
print('Hello World, \\ntoday is ' + d + '\\nand this is random - ' + n);

Now let’s compile!

C:\\myapps>jsc test.js

No error messages, so we’ll take that as an OK.

Let’s run it.

C:\\myapps>test

That’s so cool, the compiled program works!

Making a DLL

Now, we’re convinced that we have a good thing going here, so let’s create a DLL, meaning create a library that other applications such as Delphi, C#, C++ can use.

JScript.NET has the notion of namespaces and packages and class-based objects. So if we simply wrap our code in a package and a class and we create a new file LibTest.js:

package LibTest{
    class Hello {
        function sayhello() {
            var d = new Date();
            var n = Math.random();
            return 'Hello World, \\ntoday is ' + d + '\\nand this is random - ' + n;
        }
    }
}

Let’s compile this into a library, we need the /t:library option when compiling

C:\\myapps>jsc /t:library LibTest.js

Consuming the lib

Finally, let’s create an app that leverages the new library we just created.

Create consumer.js with the following:

import LibTest;
var h = new LibTest.Hello();
print(h.sayhello());

Compile and run:

C:\\myapps>jsc consumer.js
C:\\myapps>consumer.exe
Hello World,
today is Mon Aug 07 19:53:29 PDT 2017
and this is random - 0.45013379838789525