node jvm

JAVA源码 2025-08-24

Node-jvm

概述

Node -jvm -pure node.js中的jvm

例子

爪哇

 public class Main {
    public static long fib ( int n ) {
        if ( n <= 1 ) return n ;
        return fib ( n - 1 ) + fib ( n - 2 );
    }
    
    public static void main ( String [] args ) {
        if ( args . length == 0 ) {
                System . out . print ( "help: java Main.class {Number}" );
                return ;
        }
        
        int N = Integer . parseInt ( args [ 0 ]);
        long start = System . currentTimeMillis ();            
        System . out . format ( "Fibonacci from 1 to %s: n " , N );
        for ( int i = 1 ; i <= N ; i ++) {
            System . out . println ( i + ": " + fib ( i ));
        }
        long stop = System . currentTimeMillis ();
        System . out . println ( "time: " + ( stop - start ) + "ms" );
        
        System . out . println ( "done." );
    }
}

node.js

 var JVM = require ( "node-jvm" ) ;
var jvm = new JVM ( ) ;
jvm . setLogLevel ( 7 ) ;
var entryPointClassName = jvm . loadJarFile ( "./Main.jar" ) ;
jvm . setEntryPointClassName ( entryPointClassName ) ;
jvm . on ( "exit" , function ( code ) {
    process . exit ( code ) ;
} ) ;
jvm . run ( [ 15 ] ) ;

构建Java文件

cd examples/fibonacci; make

运行JVM

./fibonacci.js

干净的

make clean

输出

 Fibonacci from 1 to 15:
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34
10: 55
11: 89
12: 144
13: 233
14: 377
15: 610
time: 106ms
done.

其他例子

cd examples/

 arrays - working with different types of arrays 
dogs - simple object-oriented programming
fibonacci - recursion
jsclass - java and javascript mix
switcher - working with different switches
cast - cast for different types
ex - program exceptions
ex2 - jvm exceptions
idogs - working with interface
static - working with static objects
threads - multithreading

开发人员

Yaroslav Gaponov(Yaroslav.gaponov -at -Gmail.com)

执照

麻省理工学院许可证(MIT)

版权(C)2013 Yaroslav Gaponov

特此免费获得许可,免费授予任何获得此软件副本和相关文档文件(“软件”)的人,以无限制地处理软件,包括无限制的使用权,复制,复制,修改,合并,发布,分发,分发,分发,分配,sublicense和/或允许软件允许与以下条件相关的软件,以下是以下条件。

上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。

该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人都不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他责任,是由软件,使用或与软件中的使用或其他交易有关的。

下载源码

通过命令行克隆项目:

git clone https://github.com/YaroslavGaponov/node-jvm.git