Node.js is a very popular JavaScript runtime environment. Large web frameworks such as Vue.js and React Native run on Node.js. However, for Java programmers, compiling Node.js projects into Jar packages may be more convenient for integration into Java projects. In this article, we will introduce how to compile a Node.js project into an executable Jar package.
First, make sure you have installed Node.js and npm (the Node.js package manager). You can download the appropriate version from the official website (https://nodejs.org).
Initialize the project
mkdir node2jar cd node2jar npm init
You need to provide information about the project when executing the above command. Some default values may need to be configured by you.
Next, we need to install dependencies, such as node-jre (which can compile Node.js scripts into Java bytecode) and express.js (Express.js is a popular web application framework). Execute the following command to install dependencies:
npm install --save node-jre npm install --save express
In this step, we will write a simple Node.js script, which will output " Hello World" to the terminal. Let’s create a file called index.js and enter the following code:
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { console.log('Example app listening at http://localhost:3000'); });
Once we have After writing the Node.js script, we can use node-jre to compile it into Java bytecode. Execute the following command:
./node_modules/node-jre/bin/node-jre jre-1.8 index.js
The above command will generate a file namedserver.jar
. Execute the following command to start the server:
java -jar server.jar
At this time, visithttp://localhost:3000
and you will see the "Hello World!" output.
We can use the Exec Maven plug-in to package JRE and Node.js scripts into Jar packages.
Create a file namedassembly.xml
in the newly created project directory and enter the following content in it:
jar-with-scripts jar false ${project.basedir} / server.jar jre-1.8/**
Because our application depends on JRE , so we need to package the JRE together into a Jar package, so we include thejre-1.8
directory in the fileset.
Next, add the following content to the
tag of thepom.xml
file:
org.apache.maven.plugins maven-assembly-plugin 3.3.0 assembly.xml make-assembly package single
After that, execute the following command. It can be packaged into a Jar package:
mvn clean package
After the packaging is completed, the target Jar package can be found in thetarget
directory.
Conclusion
In this article, we introduced the method of packaging Node.js projects into Jar packages. We learned how to compile a Node.js script into Java bytecode and package it into a convenient Jar package. Now, we can easily start the Node.js server through Java'sjava -jar
command and integrate it with Java projects.
The above is the detailed content of How to compile nodejs project into jar. For more information, please follow other related articles on the PHP Chinese website!