BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Javet 3.0.2 Released: Bridging Java and JavaScript with Enhanced Features

Javet 3.0.2 Released: Bridging Java and JavaScript with Enhanced Features

Javet, a fusion of Java and V8 (JAVa + V + EighT), has recently released its version 3.0.2, marking a significant advancement in embedding Node.js and V8 in Java. This version includes Node.js v20.10.0 and V8 v12.0.267.8, highlighting the project's commitment to staying current with the latest developments in these technologies.

Consider the basic example that demonstrates embedding V8 into a Java application using Javet.

import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.V8Host;
import com.caoccao.javet.interop.V8Runtime;

public class JavetDemo {
   public static void main(String[] args) {
       try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
           // Example: Evaluating a simple JavaScript expression
           int result = v8Runtime.getExecutor("1 + 2").executeInteger();
           System.out.println("Result of 1 + 2: " + result);

           // Example: Interacting with JavaScript objects
           v8Runtime.getExecutor("const obj = { x: 10, y: 20 };").executeVoid();
           int x = v8Runtime.getExecutor("obj.x").executeInteger();
           int y = v8Runtime.getExecutor("obj.y").executeInteger();
           System.out.println("Values of x and y: " + x + ", " + y);
       } catch (JavetException e) {
           throw new RuntimeException(e);
       }
   }
}

Output:
 

Result of 1 + 2: 3
Values of x and y: 10, 20

The above example can be demonstrated using Node.js, changing one line as follows:

try (NodeRuntime node = V8Host.getNodeInstance().createV8Runtime()) {
}

The enhancement in CPU architecture support in Javet 3.0.2 is a noteworthy development. The platform now extends its compatibility to various CPU architectures, including x86_64 for Linux, macOS, and Windows and arm64 for Linux, Android, and macOS. This expansion ensures that a broader range of developers can utilize Javet across different operating systems.

A unique aspect of Javet 3.0.2 is its dynamic switching capability between Node.js and V8 mode. This flexibility, coupled with the exposure of the V8 API in the JVM, facilitates a seamless interaction between JavaScript and Java.

Consider the following example for Node.js and V8 mode:

// Node.js Mode
try (V8Runtime v8Runtime = V8Host.getNodeInstance().createV8Runtime()) {
    System.out.println(v8Runtime.getExecutor("'Hello Javet'").executeString());
}

// V8 Mode
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
    System.out.println(v8Runtime.getExecutor("'Hello Javet'").executeString());
}

This interoperability is further augmented with native support for BigInt and Date and easy integration with Spring, emphasizing the platform's commitment to a unified development experience.

The introduction of tools like JavetSanitizer for AST Analysis and support for live debugging with Chrome DevTools in Javet 3.0.2 enhances the development process. These tools make it more efficient and developer-friendly, addressing the needs of modern software development.

Moreover, the addition of the Javet engine pool in this version is a critical improvement. It facilitates better resource management by efficiently handling multiple JavaScript engines, which is crucial for complex applications requiring high performance.

Furthermore, the polyfill feature for V8 mode in Javet 3.0.2 is an essential addition. It expands the capabilities and compatibility of the V8 engine within Java, bridging gaps and ensuring smoother execution of JavaScript in a Java environment.

Setting up Javet 3.0.2 is straightforward using its Maven and Gradle Kotlin DSL dependencies. Developers can effortlessly integrate Javet into their projects, selecting the appropriate dependencies based on their target platform, be it Linux, Windows, macOS, or Android. Developers can refer to this installation guide for further details.

Prior to Javet, Nashorn was a prominent framework for embedding JavaScript in Java. It was initially introduced as part of Java 8 and provided a means to execute JavaScript code within a Java Virtual Machine (JVM). However, Nashorn's development was discontinued in Java 11, leaving a void in the landscape of Java-JavaScript integration tools. Javet steps in as an alternative, offering a robust and feature-rich solution for integrating JavaScript with Java applications.

In conclusion, Javet 3.0.2 represents a significant advancement in the convergence of Java and JavaScript. Its focus on interoperability, enhanced CPU architecture support, and developer-friendly features position it as an indispensable tool for developers engaged in both Java and JavaScript development. For additional information and to support the project, developers are encouraged to visit Javet's official website and their GitHub page. The project welcomes donations and contributions from the community to support its ongoing development​​.

About the Author

Rate this Article

Adoption
Style

BT