- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Running Java in Node.js
Node.js is an event-driven JavaScript runtime platform that is built using the Chrome’s V8 JavaScript engine. Node is mainly used for building large-scale applications. In this article, we will see how to run a Java code in Node.js.
Steps
Check if Node.js is already installed on your system using the following command −
npm init
Install Java on your local system as npm package
npm install java
Ensure that Java is already installed on your system. If not, you need to install Java on your system before running the following code.
Example 1
Create a file with the name "runJava.js" and copy the following code snippet. After creating the file, use the command "node runJava.js" to run this code.
// Importing the Java module var java = require('java'); var javaLangSystem = java.import('java.lang.System'); // Printing statement using Java in node javaLangSystem.out.printlnSync('I Love Tutorials Point <3');
Output
C:\home
ode>> node runJava.js 1.false 2.true 3.false
Example 2
Let’s have a look at one more example
// Importing the Java module var java = require('java'); var javaLangSystem = java.import('java.lang.System'); // Defining variables var a = 10; var b = 20 // Printing statement using Java in node javaLangSystem.out.printlnSync(a + ' + ' + b + " = " + (a+b));
Output
C:\home
ode>> node runJava.js 10 + 20 = 30
Advertisements