Implementation of JavaScript engine for Java which makes easier to execute scripts in Java code. Interpreting of JavaScript is assured by Rhino. Library provides especially simple and customizable way of exporting Java objects to JavaScript. Exporting is assured automatically or using special Java annotations. Interface that is used for executing scripts is familiar to JSR 223.
This library is fork of ScriptBox and is based on the abstract engine core jsen-core.
- Have installed JDK 1.6 or newer - JDK 1.8 is recommended
- Have installed Maven build manager
- Have set system variable
JAVA_HOME
to directory with installed JDK and have its binary directory in the system variablePATH
- e.g. on Windows add toPATH
variable%JAVA_HOME%\bin
(more here) - Have in the system variable
PATH
the directory with Maven installation
Simply run command: mvn package
<dependency>
<groupId>com.jsen</groupId>
<artifactId>jsen-js</artifactId>
<version>0.0.1</version>
</dependency>
Have built library (see previous section) and have it specified on classpath
public class GlobalObject {
@ScriptGetter(field="state")
public ITransformationState getState() {}
@ScriptFunction(name="write")
public void print(Object obj) throws TransformationException {}
}
GlobalObject globalObject = new GlobalObject();
AbstractScriptEngine engine = ScriptEngineManager.getInstance().getBrowserScriptEngine("text/javascript",
new GlobalObjectScriptSettings<Generator>(globalObject)
);
engine.eval(script);
For more examples see:
- Test of Java script annotations here
- Package
com.jsen.script.annotation
- Another classes like
Window
,Location
, etc. defined in ScriptBox project
If you run into any bug, please report on:
https://github.com/ITman1/jsen-js/issues
- Thrown error during
javac
build:error: annotation XYZ is missing value for the attribute <clinit>
- This error may occur if you are running Sun JDK compiler
- It is known bug: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6857918
- Solution: use JDK 8 or use different compiler than
javac
eg. Edifact Java Compiler (EJC)
Author: Radim Loskot
gmail.com: radim.loskot (e-mail)
Please feel free to contact me if you have any questions.