node-jvm实现了纯Node.js环境下的Java虚拟机。
示例:
javapublicclassMain{publicstaticlongfib(intn){if(n<=1)returnn;returnfib(n-1)+fib(n-2);}publicstaticvoidmain(String[]args){if(args.length==0){System.out.print("help:javaMain.class{Number}");return;}intN=Integer.parseInt(args[0]);longstart=System.currentTimeMillis();System.out.format("Fibonaccifrom1to%s:\n",N);for(inti=1;i<=N;i++){System.out.println(i+":"+fib(i));}longstop=System.currentTimeMillis();System.out.println("time:"+(stop-start)+"ms");System.out.println("done.");}}node.jsvarJVM=require("../../index");varjvm=newJVM();jvm.loadClassFile("./Main.class");jvm.run([10]);
评论