Jim Lawless' Blog


Rhino JavaScript to EXE with launch4j

Originally published on: Sat, 24 Jul 2010

After successfully compilng a Rhino JavaScript program to a Java class file ( Compiling Rhino JavaScript to Java), I wanted to see if I could then convert such a class file to a Windows executable (EXE) file using launch4j.

Let's start by creating a new, self-contained JS file that does not contain any runtime calls to load().

The script dir_scan.js uses a recursive function scanDir() to traverse a directory, tallying up all of the consumed file space into a map named mapOfDirs.

After the traversal, the keys from mapOfDirs are isolated into an array called mapKeys. The array is then sorted. A closure function is used in the sort invocation that sorts the array based on the corresponding value in the map. When the call to mapKeys.sort() completes, the array then holds the keys in sorted, ascending order by the computed filesize of the elements of the given directory.

dir_scan.js

You can invoke the script by using the following command-line:

java -jar js.jar dir_scan.js c:\j\rhino

The first argument is the directory that you want to scan. I supplied my Rhino installation directory. The resulting output ends with the following 10 lines:

      918278 : c:\j\rhino\javadoc\org\mozilla
     1216905 : c:\j\rhino\javadoc
     2189449 : c:\j\rhino\src\org\mozilla\javascript
     2304840 : c:\j\rhino\org\mozilla\javascript
     2306469 : c:\j\rhino\src\org
     2306469 : c:\j\rhino\src\org\mozilla
     2309824 : c:\j\rhino\src
     2349099 : c:\j\rhino\org
     2349099 : c:\j\rhino\org\mozilla
    12425293 : c:\j\rhino

After testing the script, the next step was to convert the script to a Java class file. I had run into issues in my first attempt at using launch4j until I had packaged my classes properly. When compiling dir_scan.js, I added the -package directive to force the generated class to be bound to the class com.mailsend_online.

The new output class file was then generated into the directory com\mailsend_online. I tested the class file:

java com.mailsend_online.dir_scan c:\j\rhino

I received the expected output. The next step was to jar up the class as a preparatory step for the launch4j process. I created the manifest.mf file first: manifest.mf

I tried to place the class into a jar by itself, but I ran into some CLASSPATH issues. I decompressed js.jar and js-14.jar into a tree of files starting in the current directory. I then included all of these files in my output dir_scan.jar file along with com\mailsend_online\dir_scan.class using the following command-line:

jar -cfm dir_scan.jar manifest.mf com\*.* org\*.*

Last year, when I wrote about converting using launch4j on a pure Java program ( Java in a Windows EXE with launch4j ), I used the GUI interface to bind the jar to an EXE. When I tried to do that directly this time, I was unable to make the resulting EXE appear.

I saved the configuration to a file called dir_scan.xml. I then changed the headerType element to console

dir_scan.xml

I then used the launch4j command-line utility to build the EXE.

C:\progra~1\launch4j\launch4jc dir_scan.xml
launch4j: Compiling resources
launch4j: Linking
launch4j: Wrapping
launch4j: Successfully created C:\j\rhino\dir_scan.exe

A quick test of the EXE yielded the expected results:

dir_scan c:\j\rhino

Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Compiling Rhino JavaScript to Java
Next post:Extending SpiderMonkey JavaScript on Windows


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Scott Ballantyne: Blazin' Into Forth

An Interview with Tom Zimmer: Forth System Developer

Expanding Shortened URL's

FIF Isn't Forth

PHP, Transparent GIF's, and Web Tracking

A Review of Kevin Mitnick's Book Ghost in the Wires

Book Review : Using Google App Engine

Choose your own Adventure with Sinatra

The October 10th Bug

Invoking the Default Windows Screen-Saver


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service