How to compile nodejs project into jar

PHPz
Release: 2023-05-08 14:14:38
Original
754 people have browsed it

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.

  1. Installing Node.js

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).

  1. Initialize the project

    mkdir node2jar cd node2jar npm init
    Copy after login

You need to provide information about the project when executing the above command. Some default values may need to be configured by you.

  1. Installing dependencies

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
Copy after login
  1. Write Node.js script

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'); });
Copy after login
  1. Compile Node.js Script to Java Bytecode

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
Copy after login

The above command will generate a file namedserver.jar. Execute the following command to start the server:

java -jar server.jar
Copy after login

At this time, visithttp://localhost:3000and you will see the "Hello World!" output.

  1. Dig deeper Secrets

We can use the Exec Maven plug-in to package JRE and Node.js scripts into Jar packages.

Create a file namedassembly.xmlin the newly created project directory and enter the following content in it:

 jar-with-scripts  jar  false   ${project.basedir} /  server.jar jre-1.8/**     
Copy after login

Because our application depends on JRE , so we need to package the JRE together into a Jar package, so we include thejre-1.8directory in the fileset.

Next, add the following content to thetag of thepom.xmlfile:

   org.apache.maven.plugins maven-assembly-plugin 3.3.0  assembly.xml    make-assembly package  single      
Copy after login

After that, execute the following command. It can be packaged into a Jar package:

mvn clean package
Copy after login

After the packaging is completed, the target Jar package can be found in thetargetdirectory.

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 -jarcommand 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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!