Home > Web Front-end > Front-end Q&A > How to compile nodejs project into jar

How to compile nodejs project into jar

PHPz
Release: 2023-05-08 14:14:38
Original
923 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 named server.jar. Execute the following command to start the server:

java -jar server.jar
Copy after login

At this time, visit http://localhost:3000 and 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 named assembly.xml in the newly created project directory and enter the following content in it:

<assembly>
  <id>jar-with-scripts</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>server.jar</include>
        <include>jre-1.8/**</include>
      </includes>
    </fileSet>
  </fileSets>
  <dependencySets/>
</assembly>
Copy after login

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

Next, add the following content to the <build> tag of the pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <descriptor>assembly.xml</descriptor>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
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 the target 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's java -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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template