npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node4j

v1.0.1

Published

Node4J lets Node.js apps access Java objects in a JVM as if they were native. Java methods and collections are usable with familiar JavaScript syntax. It also supports callbacks, allowing Java code to invoke Node.js functions for seamless two-way integrat

Downloads

261

Readme

Node4J

Node4J is a Node.js library that allows you to interact with Java objects as if they were native TypeScript/JavaScript objects. It acts as a bridge, enabling seamless communication between a Node.js application and a Java Virtual Machine (JVM). This project is heavily inspired by the Python library Py4J.

Features

  • Connect to a running JVM: Establish a connection to a Java application running a GatewayServer.
  • Interact with Java Objects: Call methods and access properties on Java objects directly from Node.js.
  • Asynchronous API: All interactions with the JVM are asynchronous, returning Promises for modern async/await syntax.
  • Access Static Members: Access static classes and methods, like System.out.println.
  • Create Java Objects: Instantiate new Java objects from your Node.js code.

Installation

npm install node4j

Getting Started

Using Node4J involves two parts: a Java application that acts as the server and a Node.js application that acts as the client.

1. The Java Side (Server)

Your Java application needs to run a GatewayServer to expose an entry point object to Node.js clients. This project uses the py4j library to create the gateway server.

First, add the py4j dependency to your Java project. If you are using Maven, add this to your pom.xml:

<dependency>
    <groupId>net.sf.py4j</groupId>
    <artifactId>py4j</artifactId>
    <version>0.10.9.9</version>
</dependency>

Next, create your main application class. This class will be the entry point for your Node.js application.

SampleJavaNode.java

package com.example.samplejavanode;

import py4j.GatewayServer;

/**
 *
 * @author shantanusharma01
 */
public class SampleJavaNode {

    private Stack stack;

    public SampleJavaNode() {
      stack = new Stack();
      stack.push("Initial Item");
    }

    public Stack getStack() {
        return stack;
    }

    public int addition(int first, int second) {
        return first + second;
    }

    public String sayHello(String name) {
        return "Hello, " + name + " from Java!";
    }

    public static void main(String[] args) {
        SampleJavaNode app = new SampleJavaNode();
        // The second argument is the port for the gateway server
        GatewayServer server = new GatewayServer(app, 25333);
        server.start();
        System.out.println("Gateway Server started on port 25333");
    }
}

Here is the Stack class used in the example:

Stack.java

package com.example.samplejavanode;

import java.util.LinkedList;
import java.util.List;

public class Stack {
    private List<String> internalList = new LinkedList<String>();

    public void push(String element) {
        internalList.add(0, element);
    }

    public String pop() {
        return internalList.remove(0);
    }

    public List<String> getInternalList() {
        return internalList;
    }

    public void pushAll(List<String> elements) {
        for (String element : elements) {
            this.push(element);
        }
    }
}

Run the main method in SampleJavaNode to start the server.

2. The Node.js Side (Client)

On the client side, you use the Gateway class from node4j to connect to the Java application.

Here is an example written in TypeScript:

main.ts

import { Gateway } from "node4j";

async function main() {
  // Connect to the Java Gateway Server
  const myJava = new Gateway({
    host: "127.0.0.1",
    port: 25333,
  });

  // 1. Interact with the entry point object (SampleJavaNode)
  const stack = await myJava.entryPoint.getStack();
  await stack.push("main");
  await stack.push("Second item");
  const popValue = await stack.pop(); // popValue will be "Second item"
  console.log("pop value : " + popValue);

  // 2. Create new Java objects and call static methods
  const random = await myJava.jvm.java.util.Random();
  const number1 = await random.nextInt(10);
  const number2 = await random.nextInt(10);
  console.log(`Generated numbers are: ${number1} and ${number2}`);

  const system = await myJava.jvm.System;
  const currentTime = await system.currentTimeMillis();
  console.log("java system current time : " + currentTime);
}

main().catch(console.error);

Output

pop value : Second item
Generated numbers are: 2 and 5
java system current time : 1764422092819

License

This project is licensed under the MIT License. See the LICENSE file for details.

Copyright (c) 2025 Shantanu Sharma