Java Reference
In-Depth Information
Listing 8-4.
Contents of the File Named javax.script.ScriptEngineFactory
#The factory class for the JKScript engine
com.jdojo.script.JKScriptEngineFactory
Why do you have to perform this step? You will package the
javax.script.
ScriptEngineFactory
file along with the class files for the
JKScript
engine in a JAR
file. The discovery mechanism for script engines searches for this file in the
META-INF/
services
directory in all JAR files in the CLASSPATH. If this file is found, its contents are
read and all script factory classes in this file are instantiated and included in the list of
script engine factories. Therefore, this step is necessary to make your
JKScript
engine
autodiscoverable by the
ScriptEngineManager
.
Packaging the JKScript Files
You need to package all files for the
JKScript
script engine in a JAR file named
jkscript.
jar
. You can name the file anything else as well. The following is the list of files with their
directories. Note that an empty
manifest.mf
file will work in this case:
com\jdojo\script\Expression.class
•
com\jdojo\script\JKScriptEngine.class
•
com\jdojo\script\JKScriptEngineFactory.class
•
META-INF\manifest.mf
•
META-INF\services\javax.script.ScriptEngineFactory
You can create the
jkscript.jar
file manually by copying all of these files, except
the
manifest.mf
file, into a directory, say
C:\build
on Windows, and then executing the
following command from the
C:\build
directory:
•
C:\build> jar cf jkscript.jar com\jdojo\script\*.class META-INF\services\*.*
The
jkscript.jar
file is available in the
src\JavaScripts
directory in the
downloadable package for this topic. The downloadable source code contains a
NetBeans 8.0 project and the
jkscript.jar
file is added to the project's CLASSPATH.
If you run the engine from the included NetBeans IDE, you do not need to perform the
packaging and deploying steps to use the
JKScript
engine.
Using the JKScript Script Engine
It is time to test your
JKScript
script engine. The first and most important step is to
include the
jkscript.jar
, which you created the previous section, to the application
CLASSPATH. Once you have included the
jkscript.jar
file in your application
CLASSPATH, using
JKScript
is no different from using any other script engines.
Search WWH ::
Custom Search