Using a Java Snippet from inside of Node.js

Aniruddha Chatterjee
Published in
5 min readJul 7, 2020


There are so many programming languages out there, each having its pros and cons. You cannot expect to master all of them. And you certainly cannot expect to follow the language that you prefer. Seems like an impasse, right? Not at all. While working with my friends at a hackathon, I first integrated my Node.js codes with his Python program. And since then, I never faced an issue while working with devs coding in some other language. Let us see how we can use a Java snippet right from our Node.js program.

Using multiple processes together using child_process


We shall be using Node.js as our primary program, and a simple, tiny Java Snippet to perform some actions. You do not need to know any Java. The snippet is simple enough to understand if you know the basics of programming. We shall see how to make our snippet ready from scratch, but we shall not be diving deep into Java anytime.

If you do not have Java installed, you need to follow this post to get JDK set up.

Knowing how Node.js works can be beneficial. This is an excellent post if you have no experience with Node.js or how it works. Also, do install Node.js in your system. Get it from their official site.

We will be using JavaScript as our primary language to keep things simpler. You can also do the same thing with TypeScript if you want to.

The Java Snippet

Creating our Java Snippet

Now it is the time to get our hands dirty. I will fire up my Visual Studio Code. You can use your preferred editor as well. Create a file with the following codes.

This code is easy enough to understand. We have a String array args[] which stores our command-line arguments. We are simple parsing each item and adding them to a variable sum which then we are printing on the standard output console.

Note: To keep things as simple as possible, we are not validating any input. We assume all inputs are numbers and can be parsed into integers without any errors. This, of course, should be practised in actual programs. Always validate input and handle errors if any.

Compiling and Executing our Java Snippet

Now time to compile our Java program. Type in


This will give you another file, SumOfNumbers.class which we can execute. The command for the same is

java SumOfNumbers 2 3 4

You can pass in as many numbers as you want (each separated by a space), or no number at all (in which case it will print 0).

Expected output on executing our Java Snippet

The Node.js Program

Creating our Node.js Program

Now that we have a working Java snippet, let us focus on the main Node.js program. I will start with a file mainProgram.js

Let us see what we are doing.

Step 1: We need the child_process module of Node.js in our program. Node.js runs in a single-threaded mode. We can, however, use the concepts of multi-threading using child_process. We would require a specific function spawn.

The child_process module provides the ability to spawn child processes…

We would like Node.js to spawn (or create) another process, which will work parallelly with our main-thread. One big advantage is that this does not block our main thread, and we can use callbacks as we would do for normal asynchronous processes.

Step 2: We define a function sumOfNumbers which takes in an array of numbers as the parameter. We can pass it any numbers, any number of numbers, and even pass a blank array. We just need to make sure that all the elements are numbers (since we are not validating input in our Java snippet, anything other than numbers would crash our purpose).

Step 3: We create a new array named args and push in “SumOfNumbers” as our first element. This is our Java filename and this will be picked up first by that java command. So it will be like

java SumOfNumbers <other_elememts>

Step 4: The other elements. We will now push everything from the numbers array into our args array. We are ready with our array of arguments.

Step 5: A message is printed to let us know the child process is about to start. We now create a worker, which is just a new thread for our Java process to run. The syntax is

child_process.spawn(command[, args][, options])

We are passing the command “java” and our args array as the arguments.

Step 6: We get some useful streams from spawn such as ‘stdin’, ‘stdout’ and ‘stderr’. Now we know our Java snippet will print the sum on the standard output. So we will be listening for the “data” event on ‘stdout’ stream. In simpler terms, we would like to do something (in this case, print the sum) whenever the output stream of the child gets any data.

Step 7: We would also like to print an exit message when our child process quits. So we listen for the “close” event on the child process. The “close” event gives us access to the exit code given by the child process. We print that as well.

Step 8: The final step would be to call the sumOfNumbers() functions with an array having any number of numbers (no, it is not a mistake; I meant that).

Executing our Node.js Program

Now those were some lengthy steps, right? Let us reap the fruits of our hard work, with executing the code. Type in

node mainProgram.js

And voila, we get the sum of numbers we passed in as our arguments!

Expected output of our program


We now know how to run external processes with Node.js. I used Java as an example because I have a fairly strong grasp on it. You can use any language you want like Python, C/C++ and even Bash scripts. Node.js also provides us with methods like fork() and execFile() as a part of the child_process module. We might be looking at those in some future posts. Stay everyone. Peace out.

Further Ahead

Here are some resources that might come in handy:



Aniruddha Chatterjee

Nocturnal animal that thrives on caffeine and loves software development.