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

September 1st, 2007 at 12:07 am
[...] 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 [...]
September 1st, 2007 at 1:02 am
[...] 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 [...]
September 1st, 2007 at 10:28 pm
[...] 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) [...]
September 3rd, 2007 at 3:32 pm
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
September 5th, 2007 at 12:37 am
Nice, thanks Zwetan, never heard of JSDB before…
September 19th, 2007 at 7:23 am
good text thank you
September 29th, 2007 at 4:35 pm
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)”
May 5th, 2008 at 2:59 pm
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…
August 7th, 2008 at 11:43 am
Thanks zwetan…
i got it which i want to use in my desktop and web application.
its also open source http://www.jsdb.org
August 28th, 2008 at 4:04 am
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?
August 28th, 2008 at 4:44 am
Hi Arvis, I think you should check the docs for this System.Windows.Forms package. There’s gotta be prompt, not only MessageBox
August 28th, 2008 at 6:40 am
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?
September 26th, 2008 at 4:58 pm
THANKS!!!
October 3rd, 2008 at 7:39 am
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.
October 8th, 2008 at 9:24 am
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?
February 13th, 2009 at 10:29 pm
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.
March 29th, 2009 at 9:42 pm
[...] 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 [...]
April 16th, 2009 at 4:05 am
Very good coded
thanks
April 25th, 2009 at 8:09 pm
The tips are very imformative really, thanx
May 6th, 2009 at 12:51 am
hi,
This is a very nice article.
May 18th, 2009 at 5:21 am
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?
September 3rd, 2009 at 3:48 am
[...] 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 [...]
September 11th, 2009 at 6:14 am
i think you need to reference your consumer.js
>jsc /reference:LibHello.dll consume.js
September 14th, 2009 at 3:14 pm
[...] 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 [...]
October 12th, 2009 at 5:49 am
Well, I recommend ExeScript or ExeScript Pro. Nice IDE and compilation utility, small size, full WScript support, debug, NET is NOT required
November 3rd, 2009 at 1:51 pm
[...] Ausführlicher Artikel hier Bewerte diesen Beitrag: Loading [...]
November 12th, 2009 at 2:08 pm
Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application
November 21st, 2009 at 10:57 am
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
December 1st, 2009 at 5:23 am
php, javascript, thank you because of your support on issues such as. I wish you continued success.
December 5th, 2009 at 8:17 am
Thanks! Using JSC I create my first exe application
March 1st, 2010 at 2:48 am
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
March 1st, 2010 at 7:39 am
thanks great post, always fallow your blog!
March 2nd, 2010 at 2:21 pm
nice article thank you özel güvenlik firması
March 2nd, 2010 at 2:41 pm
thank you your greatings
April 19th, 2010 at 4:23 am
beko klima servis, bakım ve montaj servisi
July 11th, 2010 at 7:11 pm
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!
July 11th, 2010 at 7:42 pm
Never mind, i got it to go and .bat ed it to compile easier.
December 8th, 2010 at 5:24 am
Excellent tips .I really appreciate all these points, and I agree completely…
85
January 2nd, 2011 at 1:33 pm
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?
January 17th, 2011 at 3:58 pm
http://www.heartratemonitorguides.com/heart-rate-monitor-watches.php Thanks for that awesome posting. It saved MUCH time
April 8th, 2011 at 8:40 am
RE: How can I supress teh DOS cmd window when a User double clicks the executable?
Compile using the /t:winexe switch — like the article says
May 20th, 2011 at 4:35 am
How can I supress teh DOS cmd window when a User double clicks the executable?
May 20th, 2011 at 4:35 am
Never mind, i got it to go and .bat ed it to compile easier.
May 20th, 2011 at 11:33 am
thanks great post, always fallow your blog!
July 19th, 2011 at 5:26 pm
[...] 「make your javascript a windows . exe」 [...]
August 23rd, 2011 at 6:42 am
Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting.
August 25th, 2011 at 12:35 am
Lol, I was about to be so sad when I first started reading your article.
I’m going to learn C++, JavaScript (already learning MooTools), PHP (know quite a bit of this one), and Python. Really, I see programming as farming or carpentry in terms of tools.
You need different tools for different types of problems and the discipline of knowing when to use each one.
August 26th, 2011 at 4:45 am
cursos Dreamweaver…
Make your javascript a Windows .exe / Stoyan’s phpied.com…
October 21st, 2011 at 6:48 am
Hi there, this is a great article, I had no idea you could do this with Javascript.
Based on your article I wrote something similar about making simple EXEs with PHP and Bamcompile –
http://pojomcbooty.tumblr.com/post/10683603233/windows-registry-scripting-with-php-compiled-to-exe
I was wondering if you know if there’s a way in either of these languages to hardcode in some other credentials to “run as”? At the moment I have to use Autohotkey for such scripting but I find it a bit flaky to be honest. If I could do it in JS or PHP I would much prefer to do so..
Thanks
November 10th, 2011 at 4:34 pm
Thank you very nice on good shares were explained.
November 10th, 2011 at 4:34 pm
Make your javascript a Windows .exe / Stoyan’s phpied.com…
November 10th, 2011 at 4:35 pm
Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting. ss
November 19th, 2011 at 4:23 am
I think that is one of the such a lot vital information for me. And i’m happy studying your article. However want to statement on some common issues, The website taste is wonderful, the articles is in reality excellent : D. Good task, cheers
January 21st, 2012 at 2:11 am
I simply could not leave your site before suggesting that I really enjoyed the usual information an individual supply for your visitors? Is going to be again steadily to inspect new posts
January 24th, 2012 at 6:24 pm
ipad…
[...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…
February 4th, 2012 at 4:12 pm
durucambalkonsistemleri…
[...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…
February 6th, 2012 at 9:40 am
Where can i get infos on making GUIs with JScript?
February 10th, 2012 at 7:55 am
I?ve been exploring for a little for any high-quality articles or weblog posts on this kind of house . Exploring in Yahoo I ultimately stumbled upon this site. Reading this info So i am happy to exhibit that I have a very just right uncanny feeling I discovered exactly what I needed. I so much indubitably will make certain to do not omit this web site and provides it a glance regularly.
February 10th, 2012 at 1:01 pm
[...] eredeti bejegyzés a JScript-ről szól: ha már tudunk JavaScript-ben programozni, miért ne ezzel végeznénk a [...]
March 4th, 2012 at 1:34 am
gazetekolik
March 14th, 2012 at 1:10 pm
C:\Users\jay\Desktop\myapps>jsc hello.js
‘jsc’ is not recognized as an internal or external command,
operable program or batch file.
March 23rd, 2012 at 9:34 am
respect to labor
Ahsap Kapi
April 26th, 2012 at 1:48 am
This is an awesome post. Please share with us the new tutorials about the coding and the features like the avatars and their updates.
jerseys hockey
April 30th, 2012 at 2:43 am
pfgrafik…
[...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…
June 4th, 2012 at 6:37 am
ingilizce eğitim seti kursu ders çalışma konusunda en iyi ingilizce dil sitesi. Kelime cümleler ve test ile ingilizce türkçe çeviri metin ve sözlük sunmaktadır.
ingilizce
November 6th, 2012 at 3:50 pm
I believe that is one of the so much significant info for me. And i am happy reading your article. However should commentary on few general things, The site taste is ideal, the articles is actually excellent : D. Good activity, cheers
March 10th, 2013 at 5:17 am
usefull article. thanks for job
March 10th, 2013 at 5:20 am
perde modelleri
March 14th, 2013 at 2:11 pm
Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting. ss
March 14th, 2013 at 2:12 pm
I believe that is one of the so much significant info for me
March 14th, 2013 at 2:15 pm
Lol, I was about to be so sad when I first started reading your article.