Background
To compile a JavaScript file into java byte code invoke the following class org.mozilla.javascript.tools.jsc.Main
In most of the time, if we want to compile the JavaScript file, we should start a Rhino session in order to invoke the org.mozilla.javascript.tools.jsc.Main class. This may be trivial if we have to compile a batch of files at a time. Thus, it's better if we can have a command instead of invoking a jar class file.
Preparation of jsc command
The followings steps are used for creating jsc command for future use:
- Enter the Java code for the Jsc class as followings:
- Save the Java file into c:\rhino\Jsc.java
- compile the Jsc.java into Jsc.class
- Enter the jsc.bat as followings:
- Save the jsc.bat to c:\rhino\jsc.bat
- Using the Administrator privilege to "right-click" the "My Computer" and select the "Properties" option
- Choose the "Advanced" tab and click the "Environment Variables" button
- Select the path variable (under System Variables is preferred),
- add ";c:\rhino" as the end
- Choose the "CLASSPATH" variable, and add ";c:\rhino" at the end
- Now, you can return to a normal user privilege
Compile a sample HelloWorld.js file
- Enter a HelloWorld.js file with the following
- Save the file to c:\js\HelloWorld.js
- invoke the "jsc HelloWorld.js" command and you may type "dir" to see the compiled HelloWorld.class file
- execute the HelloWorld.class by "java HelloWorld"
|