Download: J2V8
- j2v8_linux_x86_64. (V4.8.0)
- j2v8_win32_x86_64 (V4.6.0)
- j2v8_win32_x86 (V4.6.0)
- j2v8_macosx_x86_64 (V4.6.0)
- j2v8_android (V3.0.5)
1. Execute simple expression
1 2 3 4 5 6 7 8 9 10 11 12 | import com.eclipsesource.v8.V8; public class Expression { public static void main(String[] args) { V8 v8 = V8.createV8Runtime(); Object result = v8.executeScript("1+1"); System.out.println("Result: " + result); result = v8.executeScript("\"Hello\""); System.out.println("Result: " + result); v8.release(); } } |
Result:
Result: 2 Result: Hello
2. Execute JSON expression
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Object; public class Json { public static void main(String[] args) { V8 v8 = V8.createV8Runtime(); V8Object v8obj = v8.executeObjectScript("var me = {First: 'Robert', Middle: 'Ian', Last: 'Bull', age: 38}; me;"); String name = (String) v8obj.getString("Last"); int age = v8obj.getInteger("age"); System.out.println("Name: " + name); System.out.println("Age: " + age); v8obj.release(); v8.release(); } } |
Result:
Name: Bull Age: 38
3. Construct JavaScript Object from Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Object; import com.eclipsesource.v8.utils.V8ObjectUtils; import java.util.Map; public class JSObject { public static void main(String[] args) { V8 v8 = V8.createV8Runtime(); V8Object v8obj = new V8Object(v8); v8obj.add("First", "Robert"); v8obj.add("Last", "Bull"); v8obj.add("Age", 38); Map<String, Object> map = V8ObjectUtils.toMap(v8obj); System.out.println("Name: " + map.get("Last")); System.out.println("Age: " + map.get("Age")); v8obj.release(); v8.release(); } } |
Result:
Name: Bull Age: 38
4. Call Java function from Java Script (void)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import com.eclipsesource.v8.JavaVoidCallback; import com.eclipsesource.v8.V8; import com.eclipsesource.v8.V8Array; import com.eclipsesource.v8.V8Object; public class CallbackVoid { public static void main(String[] args) { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod(new JavaVoidCallback() { @Override public void invoke(final V8Object receiver, final V8Array args) { if (args.length() > 0) { for( int i=0; i < args.length(); i++ ) { System.out.println(args.get(i)); } } } }, "foo"); v8.executeScript("foo('a',false, undefined)"); v8.release(); } } |
Result:
a false undefined
5. Call Java function from Java Script (Integer)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import com.eclipsesource.v8.JavaCallback; import com.eclipsesource.v8.V8; public class CallbackInteger { public static void main(String[] mainArgs) { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod( (JavaCallback) (receiver, args) -> { if (args.length() > 0) { for( int i=0; i < args.length(); i++ ) { System.out.println(args.get(i)); } } return 100; }, "bar"); int v = (int)(Integer)v8.executeIntegerScript("bar('a',false, undefined)"); System.out.println("v =" + v); v8.release(); } } |
Result:
a false undefined v =100