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

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Obfuscated Ruby

Thwarting HTTP Referer Trackbacks

Embedding JavaScript in a Batch File

Hiding Batch File Console Windows

Jim Butterfield : The Commodore Guru

Extracting URL Addresses from Text in C

A Simple Associative Array Library in C

My Foray into Shareware

Stacking Images with PerlMagick

RSS feed processing with AWK


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer