Make your javascript a Windows .exe

These days an average web developer needs to have a broad matrix of skills in order to do his/her job. HTML, CSS, JavaScript, AJAX, XML, some server side language, some linux skills, some apache, some database skills, standards, accessibility, SEO, the list goes on. Parts of the list are also multiplied by (or raised to the power of?) the number of browsers you want to support. Crazy, isn't it? We're such optimists trying to make stuff work in such an environment.

There's gotta be an easier way to write code that does something meaningful! Yes, there is, it's called JavaScript. You learn JavaScript, you learn it well, and you don't need to learn anything else. Isn't that cool? JavaScript is, practically, everywhere. Learn JavaScript and you can:

  • 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)

I'm sure the list above is not even complete.

OK, it's a joke that with one programming skill only you'll be employed for life, but it's a fun thought anyway. Off to the main topic of the post.

JScript

This is Microsoft's version of JavaScript (yep, the thing that annoys us *sometimes* in IE) and can also be used to create server side pages (ASP, ASP.NET) or desktop applications. Apparently JScript is now called JScript.NET and can be compiled to create .exe files. Let's see how.

The compiler

The compiler (program that creates programs) is an exe file called jsc.exe (JScriptCompiler) and is part of the .NET framework. Good news is that you can use it without installing any MS IDE (whatever Visual Studio is called these days), free of charge. Even better, maybe it's already there, on your machine. I searched my completely normal Windows XP machine that doesn't have any special MS tools and was able to find two copies of the compiler! You can search for "jsc.exe" and in case you don't already have it, you can read how to get it here.

So once you find your jsc.exe (found one o' 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. Cool!

First .exe (in years)

The last time I created an .exe file is probably yeeears ago, when I was this Visual Basic king, writing a desktop application that takes a directory of images and creates a web gallery (example)

OK, lets create a simple application.

cd ..
mkdir myapps
cd myapps

Create a file called hello.js with the following content:

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

Now let's compile!

C:\\myapps>jsc hello.js
Microsoft (R) JScript Compiler version 8.00.50727
for Microsoft (R) .NET Framework version 2.0.50727
Copyright (C) Microsoft Corporation 1996-2005. All rights reserved.

No error messages, so we'll take that as an OK ;) Let's check:

C:\\myapps>dir
 Volume in drive C has no label.
 Volume Serial Number is B96A-95DB

 Directory of C:\\myapps

08/31/2007  07:33 PM    <DIR>          .
08/31/2007  07:33 PM    <DIR>          ..
08/31/2007  07:34 PM             4,096 hello.exe
08/31/2007  07:33 PM               109 hello.js
               2 File(s)          4,205 bytes
               2 Dir(s)  40,287,092,736 bytes free

YES! An .exe was created! Without further ado, let's run it already!

C:\\myapps>hello
Hello,
today is Fri Aug 31 19:34:32 PDT 2007
and this is random - 0.5855108083158316

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 can use.

JScript.NET has the notion of namespaces and packages (which we usually fake on the web) and class-based objects (eww! well, it supports the prototype stuff as well). So if we simply wrap our code in a package and a class and we create a new file LibHello.js:

package LibHello {
    class Hello {
        function say() {
            var d = new Date();
            var n = Math.random();
            return 'Hello, \\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 LibHello.js

This creates hello.dll and we have a library!

Consuming the lib

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

Create consumer.js with the following:

import LibHello;
var h = new LibHello.Hello();
print(h.say());

Compile and run:

C:\\myapps>jsc consumer.js

C:\\myapps>consumer.exe
Hello,
today is Fri Aug 31 19:53:29 PDT 2007
and this is random - 0.45013379838789525

Nice and easy.

So what?

I didn't have time to experiment, but I'm pretty sure you can take tools such as jsmin or jslint and easily compile them into libraries that can be consumed from windows apps, or VBA scripts in Access, Powerpoint, etc. Imagine you're writing some documentation in Word, you select some JS code you just wrote and JSlint it. That would be nice.

BTW, remember how we used /t:library option to produce a .dll and not an .exe? Well, there's also the option /t:winexe which creates a windows application I mean with the window and everything and not a console app. OK, let's give it a shot, create win.js with the following:

import System.Windows.Forms; // this has a MessageBox class
import LibHello;

var h = new LibHello.Hello();
MessageBox.Show(
        h.say(),
        "Dude!",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation
);

Compile:

C:\\myapps>jsc /t:winexe win.js

Double click in windows explorer and you have a nice little unquestionably useful Windows application ;)

This entry was posted on Friday, August 31st, 2007 and is filed under .net, JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

Somewhat related posts

38 Responses to “Make your javascript a Windows .exe”

  1. University Update - Yahoo - Make your javascript a Windows .exe Says:

    [...] YouTube Make your javascript a Windows .exe » This Summary is from an article posted at phpied.com on Friday, August 31, 2007 This article’s contents are copywritten by the author of phpied.com . Please click "View Original Article…" below to view the article. Summary Provided by Technorati.comView Original Article at phpied.com » 10 Most Recent News Articles About Yahoo [...]

  2. University Update - UN Studio - Make your javascript a Windows .exe Says:

    [...] Contact the Webmaster Make your javascript a Windows .exe » This Summary is from an article posted at phpied.com on Friday, August 31, 2007 This article’s contents are copywritten by the author of phpied.com . Please click "View Original Article…" below to view the article. Summary Provided by Technorati.comView Original Article at phpied.com » 10 Most Recent News Articles About UN Studio [...]

  3. All in a days work… Says:

    [...] Make your JavaScript a Windows .exe Can I add Windows developer to my resume? :-) Right under my nose, all that time, who’d a thunk it? (tags: JavaScript Windows) [...]

  4. zwetan Says:

    the problem with jsc.exe if that it gonna produce a .NET exe
    so anyone wanting to use that exe would need to have .NET installed

    a nice alternative that can also compile exe from javascript is JSDB :)
    http://www.JSDB.org

    it reuse the SpiderMonkey engine, does not need to have .NET installed,
    and can easyly generate an exe

    copy /b jsdb.exe+myapp.zip myapp.exe

    in myapp.zip
    your main entry point will be main.js

    so basically to have a nice exe you could do

    main.js
    —-
    var d = new Date();
    var n = Math.random();
    writeln(’Hello, \ntoday is ‘ + d + ‘\nand this is random - ‘ + n);
    —-

    then
    zip -9 -j -u myapp.zip main.js

    then
    copy /b jsdb.exe+myapp.zip myapp.exe

    then
    upx -9 myapp.exe

    :)

  5. Stoyan Says:

    Nice, thanks Zwetan, never heard of JSDB before…

  6. perde Says:

    good text thank you

  7. Conrad Says:

    This is great!!
    All seems to work however I wanted to use the dll as a component in a web page. So the source code is protected and other reasons. However i could not use the “regsvr32″. After some reading I found out about “regasm”.
    Finally I ran the ASP page but got an error with no real details??
    “Server object, ASP 0177 (0×80070002)”

  8. iCE Says:

    Another possible alternative is ScriptCryptor from http://www.abyssmedia.com/scriptcryptor/
    It support built-in WScript object and create standalone win32 executables with customizable resources like Version Info, Icon…
    But its not freeware…

  9. Khimdhua Says:

    Thanks zwetan…
    i got it which i want to use in my desktop and web application.
    its also open source http://www.jsdb.org

  10. Arvis Lacis Says:

    Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that?

  11. Stoyan Says:

    Hi Arvis, I think you should check the docs for this System.Windows.Forms package. There’s gotta be prompt, not only MessageBox ;)

  12. Arvis Lācis Says:

    Ok, I try ListBox.Show() and TextBox.Show(), but then, when I complite my script I get error: “error JS1246: Type ‘System.Windows.Forms.ListBox’ does not have such a static member”! What it mean and how can I fix this error?

  13. GANDALF Says:

    THANKS!!!

  14. nyturn Says:

    I use ExeScript from http://www.scriptcode.com/ to convert scripts to exe. It encrypts file content to protect it from modification by other users.

  15. devodl Says:

    Nice article.
    I tested the example shown below and it works fine.

    import System.Windows.Forms;
    System.Windows.Forms.MessageBox.Show(”Welcome! Press OK to continue.”);
    MessageBox.Show(”Great! Now press OK again.”);

    However when I double click on the executable in Windows Explorer it produces a DOS cmd window.
    How can I supress teh DOS cmd window when a User double clicks the executable?

  16. tuba buyukustun Says:

    Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that? Thank you.

  17. Advocacy of JavaScript « Lea Verou Says:

    [...] You are wrong, again. You can code in JavaScript for the server, create Windows executable files (.exe), create  plugins and extensions for a plethora of applications, and actually even Flash’s [...]

  18. mobilya Says:

    Very good coded :) thanks

  19. Hastane Says:

    Thank you very much very nice article
    Great information! Very useful for me. Thanks a lot.
    The idea is awesome. Congrats.

  20. perde Says:

    The tips are very imformative really, thanx

  21. Manoj.CM Says:

    hi,

    This is a very nice article.

  22. araç kiralama Says:

    Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that?

  23. JavaScript shell scripting / phpied.com Says:

    [...] for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I’m going with this. JavaScript is [...]

  24. hmulyadi Says:

    i think you need to reference your consumer.js
    >jsc /reference:LibHello.dll consume.js

  25. JavaScript shell scripting | pc-aras Says:

    [...] for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I’m going with this. JavaScript is [...]

  26. Vital Says:

    Well, I recommend ExeScript or ExeScript Pro. Nice IDE and compilation utility, small size, full WScript support, debug, NET is NOT required

  27. René Bühlings WebBlog » Blog Archiv » JavaScript exe und dll Compiler Says:

    [...] Ausführlicher Artikel hier Bewerte diesen Beitrag:  Loading [...]

  28. perde modelleri Says:

    Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  29. cam filmi Says:

    thank you ;) Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  30. senao Says:

    php, javascript, thank you because of your support on issues such as. I wish you continued success.

  31. Perde Modelleri Says:

    Thanks! Using JSC I create my first exe application

  32. çocuk oyun parkı Says:

    Hi everybody. Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  33. Kırtasiye Mazemeleri Says:

    thanks great post, always fallow your blog!

  34. özel güvenlik firması Says:

    nice article thank you özel güvenlik firması

  35. özel güvenlik firmaları Says:

    thank you your greatings

  36. beko klima servis Says:

    beko klima servis, bakım ve montaj servisi

  37. John Says:

    What should the environment variable PATH Be changed to?
    GIVE EVERYTHING NEEDED OR TO KNOW OR TELL WHERE TO FIND IT OR DON’T TRY TO HELP AT ALL!

  38. John Says:

    Never mind, i got it to go and .bat ed it to compile easier.

Leave a Reply