SlideShare a Scribd company logo
Node4J
Running Node.js in a Java
World
Dr. R. Ian Bull
EclipseSource
@irbull
Java and JavaScript
❖ Java a successful server side language
❖ JavaScript is a client side language
❖ SWT brought performant Java UIs to the desktop
❖ Node.js brought JavaScript to the server
❖ Java and JavaScript are two of the most popular
programming languages
Polyglot Systems
❖ Single language systems are rarely an option
❖ Legacy code
❖ New frameworks and technologies
❖ Evolving enterprises
❖ JEE will be here for another 20, 30, 50 (?) years
Bridging Java and JavaScript
❖ Three common Java technologies enable JS embedding
❖ Rhino
❖ Available since JDK 6
❖ Nashorn
❖ Replacing Rhino since JDK 8
❖ More performant
❖ V8 as a separate process, String based messages
Performance
❖ 30 Runs of the Esprima parser and tokenizer
❖ Nashorn compiles to bytecode
❖ V8 compiles to native assembly
❖ Best choice for raw JavaScript execution
Node4J: Running Node.js in a JavaWorld
J2V8
❖ A set of bindings that bring V8 to Java
❖ Inspired by SWT
❖ Create a thin JNI layer
❖ Expose (some) V8 API in Java
❖ Complicated logic lives in Java
J2V8 Goals
❖ Efficient JavaScript on Android
❖ Make JavaScript shine in an enterprise Java World
❖ Standard Java APIs
❖ Efficient Java / JavaScript bindings
J2V8 — History
❖ 1.0 Released in November 2014
❖ 2.0 Released in February 2015
❖ First presented at EclipseCon 2015
❖ 3.0 Released at EnterJS — Summer 2015
J2V8 Design
❖ Each V8 Object can be referenced using a Handle
❖ Each Object is stored in a V8 Persistent Object Store
❖ Objects must be explicitly freed
❖ Primitives where possible (no wrappers)
❖ Single Thread per isolate
Two-way binding
❖ JS functions and scripts can be invoked from Java
❖ Java methods can be called from JavaScript
❖ Data can be passed back and forth using V8Objects
J2V8 In Action — Tabris.js
❖ Mobile framework
❖ Apps written in JavaScript
❖ Native iOS and Android Apps
❖ Bindings to native UI components
Shameless Plug
Example
public String someJavaMethod(final String firstName, final String lastName) {
return firstName + ", " + lastName;
}
public void start() {
V8 v8 = V8.createV8Runtime();
v8.registerJavaMethod(this,
"someJavaMethod",
"someJavaMethod",
new Class[] { String.class, String.class });
v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');");
String result = v8.getString("result");
System.out.println(result);
}
J2V8 —What’s New
❖ Typed Arrays
❖ Threads & Workers
❖ ES 6
❖ ChromeDev Tools
❖ NodeJS Support
Typed Arrays
V8Array result = (V8Array) v8.executeScript(""
+ "var buf = new ArrayBuffer(100);"
+ "var ints = new Int32Array(buf); "
+ "for(var i = 0; i < 25; i++) {"
+ " ints[i] = i;"
+ "}; "
+ “ints");
int[] ints = result.getIntegers(0, 25);
❖ Native support for JS Typed Arrays
❖ Access the values efficiently from Java
Threads
❖ Every thread can have it’s own Isolate (Isolated V8
Instance)
❖ V8Thread is a Java Thread with an associated Isolate
❖ Provide an easy way to execute JavaScript
Thread t = new V8Thread(new V8Runnable() {
public void run(V8 v8) {
int result = v8.executeIntegerScript("1+2");
}
});
t.start();
Executors
❖ Long running V8Thread with a message queue and
event loop
❖ Threads can communicate via message passing
❖ Useful for implementing Web Workers / Service
Workers
ES 6
❖ Snapshot builds of J2V8 support V8 4.10 & ES 6
❖ Arrows
❖ Classes
❖ Let / Const
❖ Interators + For..Of
❖ Generators
❖ …
Debug Support
❖ V8 (and now J2V8) no longer supports the Debug Agent
❖ JavaScript based Debug API is available instead
❖ J2V8 exposes this API in Java
❖ Integrated with the Stetho tool & Chrome Dev Tools
Debug Support Demo
Node.js
❖ JavaScript Virtual Machine (V8)
❖ Modules
❖ Native
❖ JavaScript
❖ Event Loop
Node.js® is a JavaScript runtime built on Chrome's V8
JavaScript engine. Node.js uses an event-driven,
non-blocking I/O model that makes it
lightweight and efficient.
Bridging to Node.js
❖ Out of process Node & REST Services
❖ Vert.x
❖ Node engine on Nashorn / Rhino?
Node4J
❖ Dynamically link Node.js to the JVM
❖ Access Node.js context via JNI
❖ Execute Node.js modules (require)
❖ Callbacks to Java
❖ Process Node.js message queue
Node4J Demo
public static void main(final String[] args) throws Exception {
final V8 v8 = V8.createV8Runtime("global");
v8.registerJavaMethod(…);
NodeJS node = V8.createNodeJS(v8);
V8Object exports = node.requireScript(nodeCode, "http");
exports.release();
boolean running = true;
while (running) {
running = node.pumpMessageLoop();
}
}
Performance Considerations
❖ Minimize callbacks from JavaScript to Java
❖ ~4000 Per Second on my MBP
❖ Use bulk array copy to move primitives from JS to Java
❖ 60fps in our animation demo
Resources
❖ Getting started with J2V8
❖ Registering Java Callbacks with J2V8
❖ Implementing WebWorkers with J2V8
❖ Multithreaded JavaScript with J2V8
❖ Using J2V8 with Heroku
❖ All linked from our GitHub Page
Future Work
❖ Advanced exception handling between Java and JS
❖ Improved debug support
❖ Typed array access in Java
❖ You tell me?
Using J2V8
❖ J2V8 is available in Maven Central
❖ Currently 5 variants are available:
com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6

com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6

com.eclipsesource.j2v8.j2v8:3.1.6 (aar)

com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6

com.eclipsesource.j2v8.j2v8_android_x86:3.1.6
❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
4.0!
Thank-you
❖ Open Source Java bindings for V8
❖ Node4J extensions bring Node.js to Java
❖ Licensed under the EPL
❖ For J2V8 news, follow me on Twitter @irbull
https://github.com/eclipsesource/j2v8
Node4J: Running Node.js in a JavaWorld

More Related Content

PDF
Unrevealed Story Behind Viettel Network Cloud Hotpot | Đặng Văn Đại, Hà Mạnh ...
PDF
Jfrog artifactory as private docker registry
PPT
Tomcat Server
PPTX
Vmware Tanzu Kubernetes Connect(Spanish)
PDF
Deployment Patterns in WSO2 Enterprise Integrator
PDF
Authentification et autorisation d'accès avec AWS IAM
PDF
Nova: Openstack Compute-as-a-service
PPTX
Canary Releases on Kubernetes w/ Spinnaker, Istio, and Prometheus
Unrevealed Story Behind Viettel Network Cloud Hotpot | Đặng Văn Đại, Hà Mạnh ...
Jfrog artifactory as private docker registry
Tomcat Server
Vmware Tanzu Kubernetes Connect(Spanish)
Deployment Patterns in WSO2 Enterprise Integrator
Authentification et autorisation d'accès avec AWS IAM
Nova: Openstack Compute-as-a-service
Canary Releases on Kubernetes w/ Spinnaker, Istio, and Prometheus

What's hot (19)

PPTX
Azure Container Apps
PDF
Docker Birthday #3 - Intro to Docker Slides
PDF
SpringBoot 3 Observability
PDF
Aryaka Bringing SASE to Life with a Zero Trust WAN.pdf
PDF
Qualys Brochure for CISOs
PPT
Ansible presentation
PDF
왜 컨테이너인가? - OpenShift 구축 사례와 컨테이너로 환경 전환 시 고려사항
PDF
OSGi Blueprint Services
ODP
Introduction to Ansible
PDF
Cloud Native Security: New Approach for a New Reality
PDF
PDF
OPENSHIFT CONTAINER PLATFORM CI/CD Build & Deploy
PDF
Open shift 4 infra deep dive
PPTX
SAP HANA & S/4HANA: How hackers are compromising S/4HANA and how you can prot...
PPTX
Ansible presentation
PPTX
2015/06/12 - IBM Systems & Middleware - IBM DataPower and API Management
PPTX
Docker: From Zero to Hero
PDF
High availability virtualization with proxmox
PDF
“How to Secure Your Applications With a Keycloak?
Azure Container Apps
Docker Birthday #3 - Intro to Docker Slides
SpringBoot 3 Observability
Aryaka Bringing SASE to Life with a Zero Trust WAN.pdf
Qualys Brochure for CISOs
Ansible presentation
왜 컨테이너인가? - OpenShift 구축 사례와 컨테이너로 환경 전환 시 고려사항
OSGi Blueprint Services
Introduction to Ansible
Cloud Native Security: New Approach for a New Reality
OPENSHIFT CONTAINER PLATFORM CI/CD Build & Deploy
Open shift 4 infra deep dive
SAP HANA & S/4HANA: How hackers are compromising S/4HANA and how you can prot...
Ansible presentation
2015/06/12 - IBM Systems & Middleware - IBM DataPower and API Management
Docker: From Zero to Hero
High availability virtualization with proxmox
“How to Secure Your Applications With a Keycloak?
Ad

Similar to Node4J: Running Node.js in a JavaWorld (20)

PDF
Running JavaScript Efficiently in a Java World
PDF
Gluecon 2014 - Bringing Node.js to the JVM
PPTX
[2014.11.18] java script execution environment survey
PDF
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
PPTX
Difference between Node.js vs Java script
PPTX
Node js for beginners
PPTX
NodeJS - Server Side JS
PPTX
Nashorn
PDF
JDD2015: Java Everywhere Again—with DukeScript - Jaroslav Tulach
PPTX
concept of server-side JavaScript / JS Framework: NODEJS
PDF
Node.js vs. java
PDF
JAX London 2015: Java vs Nodejs
PDF
Avatar 2.0
PPTX
Next-generation JavaScript - OpenSlava 2014
PDF
Nodejs
PDF
Node.js vs. java which one should you choose for backend development
PDF
Java vs. Java Script for enterprise web applications - Chris Bailey
PDF
Node js (runtime environment + js library) platform
PDF
InterConnect2016: WebApp Architectures with Java and Node.js
PPTX
Irfan maulana nodejs web development
Running JavaScript Efficiently in a Java World
Gluecon 2014 - Bringing Node.js to the JVM
[2014.11.18] java script execution environment survey
IBM InterConnect: Java vs JavaScript for Enterprise WebApps
Difference between Node.js vs Java script
Node js for beginners
NodeJS - Server Side JS
Nashorn
JDD2015: Java Everywhere Again—with DukeScript - Jaroslav Tulach
concept of server-side JavaScript / JS Framework: NODEJS
Node.js vs. java
JAX London 2015: Java vs Nodejs
Avatar 2.0
Next-generation JavaScript - OpenSlava 2014
Nodejs
Node.js vs. java which one should you choose for backend development
Java vs. Java Script for enterprise web applications - Chris Bailey
Node js (runtime environment + js library) platform
InterConnect2016: WebApp Architectures with Java and Node.js
Irfan maulana nodejs web development
Ad

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
ai tools demonstartion for schools and inter college
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Essential Infomation Tech presentation.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PPTX
L1 - Introduction to python Backend.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Understanding Forklifts - TECH EHS Solution
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
ai tools demonstartion for schools and inter college
Design an Analysis of Algorithms I-SECS-1021-03
AI in Product Development-omnex systems
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Essential Infomation Tech presentation.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
L1 - Introduction to python Backend.pptx
Softaken Excel to vCard Converter Software.pdf
Odoo POS Development Services by CandidRoot Solutions
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Understanding Forklifts - TECH EHS Solution
PTS Company Brochure 2025 (1).pdf.......
Design an Analysis of Algorithms II-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia

Node4J: Running Node.js in a JavaWorld

  • 1. Node4J Running Node.js in a Java World Dr. R. Ian Bull EclipseSource @irbull
  • 2. Java and JavaScript ❖ Java a successful server side language ❖ JavaScript is a client side language ❖ SWT brought performant Java UIs to the desktop ❖ Node.js brought JavaScript to the server ❖ Java and JavaScript are two of the most popular programming languages
  • 3. Polyglot Systems ❖ Single language systems are rarely an option ❖ Legacy code ❖ New frameworks and technologies ❖ Evolving enterprises ❖ JEE will be here for another 20, 30, 50 (?) years
  • 4. Bridging Java and JavaScript ❖ Three common Java technologies enable JS embedding ❖ Rhino ❖ Available since JDK 6 ❖ Nashorn ❖ Replacing Rhino since JDK 8 ❖ More performant ❖ V8 as a separate process, String based messages
  • 5. Performance ❖ 30 Runs of the Esprima parser and tokenizer ❖ Nashorn compiles to bytecode ❖ V8 compiles to native assembly ❖ Best choice for raw JavaScript execution
  • 7. J2V8 ❖ A set of bindings that bring V8 to Java ❖ Inspired by SWT ❖ Create a thin JNI layer ❖ Expose (some) V8 API in Java ❖ Complicated logic lives in Java
  • 8. J2V8 Goals ❖ Efficient JavaScript on Android ❖ Make JavaScript shine in an enterprise Java World ❖ Standard Java APIs ❖ Efficient Java / JavaScript bindings
  • 9. J2V8 — History ❖ 1.0 Released in November 2014 ❖ 2.0 Released in February 2015 ❖ First presented at EclipseCon 2015 ❖ 3.0 Released at EnterJS — Summer 2015
  • 10. J2V8 Design ❖ Each V8 Object can be referenced using a Handle ❖ Each Object is stored in a V8 Persistent Object Store ❖ Objects must be explicitly freed ❖ Primitives where possible (no wrappers) ❖ Single Thread per isolate
  • 11. Two-way binding ❖ JS functions and scripts can be invoked from Java ❖ Java methods can be called from JavaScript ❖ Data can be passed back and forth using V8Objects
  • 12. J2V8 In Action — Tabris.js ❖ Mobile framework ❖ Apps written in JavaScript ❖ Native iOS and Android Apps ❖ Bindings to native UI components
  • 14. Example public String someJavaMethod(final String firstName, final String lastName) { return firstName + ", " + lastName; } public void start() { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod(this, "someJavaMethod", "someJavaMethod", new Class[] { String.class, String.class }); v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');"); String result = v8.getString("result"); System.out.println(result); }
  • 15. J2V8 —What’s New ❖ Typed Arrays ❖ Threads & Workers ❖ ES 6 ❖ ChromeDev Tools ❖ NodeJS Support
  • 16. Typed Arrays V8Array result = (V8Array) v8.executeScript("" + "var buf = new ArrayBuffer(100);" + "var ints = new Int32Array(buf); " + "for(var i = 0; i < 25; i++) {" + " ints[i] = i;" + "}; " + “ints"); int[] ints = result.getIntegers(0, 25); ❖ Native support for JS Typed Arrays ❖ Access the values efficiently from Java
  • 17. Threads ❖ Every thread can have it’s own Isolate (Isolated V8 Instance) ❖ V8Thread is a Java Thread with an associated Isolate ❖ Provide an easy way to execute JavaScript Thread t = new V8Thread(new V8Runnable() { public void run(V8 v8) { int result = v8.executeIntegerScript("1+2"); } }); t.start();
  • 18. Executors ❖ Long running V8Thread with a message queue and event loop ❖ Threads can communicate via message passing ❖ Useful for implementing Web Workers / Service Workers
  • 19. ES 6 ❖ Snapshot builds of J2V8 support V8 4.10 & ES 6 ❖ Arrows ❖ Classes ❖ Let / Const ❖ Interators + For..Of ❖ Generators ❖ …
  • 20. Debug Support ❖ V8 (and now J2V8) no longer supports the Debug Agent ❖ JavaScript based Debug API is available instead ❖ J2V8 exposes this API in Java ❖ Integrated with the Stetho tool & Chrome Dev Tools
  • 22. Node.js ❖ JavaScript Virtual Machine (V8) ❖ Modules ❖ Native ❖ JavaScript ❖ Event Loop Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • 23. Bridging to Node.js ❖ Out of process Node & REST Services ❖ Vert.x ❖ Node engine on Nashorn / Rhino?
  • 24. Node4J ❖ Dynamically link Node.js to the JVM ❖ Access Node.js context via JNI ❖ Execute Node.js modules (require) ❖ Callbacks to Java ❖ Process Node.js message queue
  • 25. Node4J Demo public static void main(final String[] args) throws Exception { final V8 v8 = V8.createV8Runtime("global"); v8.registerJavaMethod(…); NodeJS node = V8.createNodeJS(v8); V8Object exports = node.requireScript(nodeCode, "http"); exports.release(); boolean running = true; while (running) { running = node.pumpMessageLoop(); } }
  • 26. Performance Considerations ❖ Minimize callbacks from JavaScript to Java ❖ ~4000 Per Second on my MBP ❖ Use bulk array copy to move primitives from JS to Java ❖ 60fps in our animation demo
  • 27. Resources ❖ Getting started with J2V8 ❖ Registering Java Callbacks with J2V8 ❖ Implementing WebWorkers with J2V8 ❖ Multithreaded JavaScript with J2V8 ❖ Using J2V8 with Heroku ❖ All linked from our GitHub Page
  • 28. Future Work ❖ Advanced exception handling between Java and JS ❖ Improved debug support ❖ Typed array access in Java ❖ You tell me?
  • 29. Using J2V8 ❖ J2V8 is available in Maven Central ❖ Currently 5 variants are available: com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6
 com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6
 com.eclipsesource.j2v8.j2v8:3.1.6 (aar)
 com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6
 com.eclipsesource.j2v8.j2v8_android_x86:3.1.6 ❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
  • 30. 4.0!
  • 31. Thank-you ❖ Open Source Java bindings for V8 ❖ Node4J extensions bring Node.js to Java ❖ Licensed under the EPL ❖ For J2V8 news, follow me on Twitter @irbull https://github.com/eclipsesource/j2v8