npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodejs-jre

v2.0.1

Published

Embed JRE or JDK into nodejs application and make it easy to use.

Downloads

3

Readme

nodejs-jre

Nodejs-jre 可以从开源镜像网站下载 Java 运行时环境(JRE)或 Java 开发工具包(JDK)嵌入你的 node.js 应用并提供使用方法。

当前支持的 JRE、JDK 版本:8111718192021

安装

npm install --save nodejs-jre

使用

安装 nodejs-jre,会自动下载 jre8。若这不是你想要的资源类型或版本,可调用 install API 下载对应资源。(同种资源类型的下载会进行覆盖,不同种的可以共存,例如:你不能同时拥有 jre8jre11,但你可以同时拥有 jre8jdk11

const { install } = require('nodejs-jre');

// 安装 jre17
install('jre', 17);

// 安装 jdk11,并在安装完成后输出:jdk11 安装成功
install('jdk', 11).then(() => {
    console.log('jdk11 安装成功');
});

需要注意的是,除 installnodejs-jre 的其他 API 都需要有对应资源才能使用。例如,要用 jre.java 就需确保安装 JRE,要用 jdk.javac 就需确保安装 JDK。

接下来我们通过 Java 的 “Hello World” 程序了解 nodejs-jre 的基本用法。首先我们在根目录下的 test 文件夹中准备一个原始的 Java 文件,test/Hello.java

public class Hello {
    public static void main(String[] args) {
        System.out.print("Hello " + args[0] + "!");
    }
}

通过运行如下代码,将 test/Hello.java 编译成 class 文件并运行。

const { jre, jdk } = require('nodejs-jre');

// 同步编译
jdk.javacSync('test/Hello.java');

// 异步运行并取得结果
let outcome = '';
const child = jre.java('Hello', ['-cp', 'test'], ['world']);

child.stdout.on('data', data => {
    outcome = data;
});
child.stderr.on('data', data => {
    console.log('Error: ' + data);
})
child.on('close', code => {
    console.log(outcome);   // Hello world!
})

API

Nodejs-jre 的 API 基本都是基于 child_process.spawnchild_process.spawnSync 的包装。除 install 外,所有 API 都提供同步和异步两个版本,它们拥有同样的参数,同步 API 具体可参考 child_process.spawnSync

install(driver, version)

异步下载指定版本的 JRE 或 JDK 集成到项目中去,该方法返回一个在集成完成后转为 fulfilledPromise。在 npm i nodejs-jre 时,会自动调用 install('jre', 8) 下载安装 jre8

参数:

  • driver {String} — 资源类型
    • 必填,仅支持 'jre''jdk'
  • version {String | Number} — 资源版本
    • 必填,目前仅支持 8111718192021

jre.java(source[, args][, execArgs][, options])

加载指定的类或文件,运行 Java 程序。具体用法可参考官方文档

参数:

  • source {String} — 要启动的类名或 jar 文件,需搭配不同的 args 使用
    • 必填,例如:'Hello''xxx.jar'
  • args {String[]} — java 使用的命令行选项
    • 选填,默认:[]
    • 例如:['-cp', 'test']['-jar', 'xxx.jar']、...
    • 查看所有可使用的选项列表
  • execArgs {String[]} — 传递给主类的参数
    • 选填,默认:[]
    • 例如:['world']、...
  • options {Object} — 传递给 child_process.spawnoptions 部分使用的选项
    • 选填,默认:{ encoding: 'utf-8' }
    • 查看所有可使用的选项列表

该函数返回一个 ChildProcess 实例,用以处理进程的执行结果和错误信息,具体可参考 child_process.spawn

const { jre } = require('nodejs-jre');

/*** Some Examples ***/

// 合并父进程与子进程的环境变量,运行 Hello.class 文件
jre.java('Hello', [], [], { env: ...process.env });  

// 运行 Test.jar 文件
jre.java('Test.jar', ['-jar']);

// windows 中运行 a.jar 文件中的 org.xxx.yyy.ZZZ 类
// 同时传递 '12'、'34' 两个参数给它
jre.java('org.xxx.yyy.ZZZ', ['-cp', ';.jar/a.jar'], ['12', '34']);  

jdk.javac(sourceFile[, args][, options])

读取 Java 类和接口定义,并将它们编译为字节码和类文件。具体用法可参考官方文档

参数:

  • sourceFile {String | String[]} — 一个或多个要编译的源文件,有多个时以数组形式传入
    • 必填,例如:'Hello.java'['Hello.java', 'World.java']
  • args {String[]} — javac 使用的命令行选项
    • 选填,默认:[]
    • 例如:['-d', 'test']
    • 查看所有可使用的选项列表
  • options {Object} — 传递给 child_process.spawnoptions 部分使用的选项
    • 选填,默认:{ encoding: 'utf-8' }
    • 查看所有可使用的选项列表

该函数返回一个 ChildProcess 实例,用以处理进程的执行结果和错误信息,具体可参考 child_process.spawn

const { jdk } = require('nodejs-jre');

/*** Some Examples ***/

// 合并父进程与子进程的环境变量,编译 test 文件夹下的 Hello.java 文件
jdk.javac('test/Hello.java', [], { env: ...process.env });  

// 编译 test 文件夹下的 Hello.java 和当前目录下的 World.java 文件
// 在 class 文件夹下生成编译好的文件
jdk.javac(['test/Hello.java', 'World.java'], ['-d', 'class']);

jdk.jar(mode, jarPath[, args][, options])

为类和资源创建 jar 包或操纵 jar 包中的类和资源。具体用法可参考官方文档

参数:

  • mode {String} — 主要操作模式
    • 必填,例如:'tf'-cf、...
  • jarPath {String} — 要操作的 jar 文件路径
    • 必填,例如:'jars/xxx.jar'
  • args {String | String[]} — jar 使用的命令行参数
    • 选填,默认:[]
    • 例如:'class/xxx.class'['--manifest', 'mymanifest', '-C', 'foo/']、...
    • 查看所有可使用的参数列表
  • options {Object} — 传递给 child_process.spawnoptions 部分使用的选项
    • 选填,默认:{ encoding: 'utf-8' }
    • 查看所有可使用的选项列表

该函数返回一个 ChildProcess 实例,用以处理进程的执行结果和错误信息,具体可参考 child_process.spawn

const { jdk } = require('nodejs-jre');

/*** Some Examples ***/

// 列出当前目录下 test.jar 的文件目录
jdk.jar('-tf', './test.jar');

// 将 class 文件夹下的 a、b 两个 class 文件打成 jar 包,放在 jars 目录下命名为 test.jar
jdk.jar('cf', 'jars/test.jar', ['class/a.class', 'class/b.class']);

扩展

如果你需要的命令不在现有 API 中,可以通过 jre.binjdk.bin 访问已安装的 JREJDKbin 目录进行扩展。

闲聊

作者 Duskstar,一个闲云野鹤般的前端攻城狮。偶尔玩玩开源项目,也偶尔写写博客

若对该项目有疑惑,或有心交流技术,不妨交个朋友🍻:

人生难得,因缘际会,便随缘乐助留下 Star ⭐,不胜感激。

License

MIT Copyright (c) 2023 Duskstar