Over the years, the rise of node. js, JavaScript has changed from "The world's most misunderstood language" to "the world's most popular language." and its development, from the evolution of language itself, the growth of libraries and packages, the improvement of tool support, star project and the emergence of domain solutions, platforms, technology stacks, the expansion of applications, and so on, are not seen before. With Node.js
the popularity of services, for the back-end service is the Java company will have a problem, Node.js
how to Java
communicate with it?
What are you talking about today?
We put aside the architectural design, aside from the http
traditional socket
, rpc
communication protocols, we talk Node.js
about how to connect in the application Java API
, in other words, Node.js
write code directly in Java
.
Node-java
Node.js
Want and Java
Connect, need a Node-java module.
Environment preparation
Liunx If you do not support C + + 11 You need to manually upgrade GCC to gcc 4.8.) Reference Address
If this is a Mac
hint: Display needs to install the old Java SE 6 operating environment to open, download JDK 2015
node. JS calls Javahelloworld
Public class HelloWorld { publicstaticvoid main (string[] args) { System.out.println ("Hello world!" ); }}
Output: Hello world!
Const Java = require (' java '= Java.import (' Java.lang.System ') javaLangSystem.out.printlnSync (' Hello world! ')
Output: Hello world!
Operation Java Mapjava Operation HashMap
ImportJava.util.HashMap;ImportJava.util.Map; Public classHashmapdemo { Public Static voidMain (string[] args) {Map<string, object> map =NewHashmap<>(); Map.put ("Name", "Sunilwang"); Map.put ("Age", 20); String name= (String) map.get ("name"); intAge = (int) Map.get ("Age"); System.out.printf ("Name:%s", name); System.out.println (""); System.out.printf ("Age:%d", age); }}
Output: Name:sunilwang age:20
node. JS synchronizes calls to Java HashMap
Const Java = require (' java '= Java.import (' java.util.HashMap ')// synchronous operation New HashMap () hashmap.putsync(' name ', ' Sunilwang ') hashmap.putsync (' age ', Hashmap.getsync (' name '= Hashmap.getsync (' age ') console.log (' name ', name) Console.log ( "Age", age)
Output: Name:sunilwang age:20
node. JS Callback calls Java HashMap
Const Java = require (' java '= Java.import (' java.util.HashMap ')// callback Operation New HashMap () hashmap.put (' name ', ' Sunilwang ', (error, info) = = {if ( Error) Console.log (' Put name ' ERROR: ', error ') hashmap.get (' name ', (error, name) = = { if (Error) Console.log (' Get name error: ', error) Console.log (' callback name:%s ' , Name)}) })
Output: Callback Name:sunilwang
node. JS Promise calls Java HashMap
Const CO = require (' Co ') const Java= require (' Java ')//the current configuration must be declared at the top of theJava.asyncoptions ={syncsuffix:' Sync ',//Synchronous method name suffixAsyncsuffix: ",//Async method Name suffixPromisesuffix: ' Promise ',//Promise method Name suffixPromisify:require (' Bluebird '). promisify//modules that rely on promise}// ==============Const HASHMAP= Java.import (' Java.util.HashMap ')//Promise OperationCofunction*() {Let HashMap=NewHashMap () yield hashmap.putpromise (' Name ', ' Sunilwang ') yield hashmap.putpromise (' Age ', ' 20 ') let name= Yield hashmap.getpromise (' name ') let-age= Yield hashmap.getpromise (' age ') Console.log (' Name ', name) Console.log (' Age ', Age)})
Two methods of creating Java instances
Importjava.util.ArrayList;Importjava.util.List; Public classArraylistdemo { Public Static voidMain (string[] args) {List<String> List1 =NewArraylist<>(); List<String> List2 =NewArraylist<>(); List1.add ("Item1"); List2.add ("Item1"); System.out.printf ("Size:%d", list1.size ());//2System.out.println (""); //list1 equals List2:trueSystem.out.printf ("List1 equals List2:%s", List1.equals (List2)); }}
Newinstancesync
Const Java = require (' java '= Java.newinstancesync (' java.util.ArrayList '// 0 List1.addsync (' item1 '// 1
Import & New
Let ArrayList = java. Import (' java.util.ArrayList 'new ArrayList () list2.addsync (' item1 '= List2.equalssync (List1) console.log (equalvalue)// true
Other operations quick new Data group
Public class Chararraydemo { publicstaticvoid main (string[] args) { Char [] chararray = "Hello world\n". ToCharArray (); // Chararray length:12 System.out.printf ("Chararray Length:%d", chararray.length);} }
Let Chararray = Java.newarray (' char ', ' Hello world\n '. Split ('))// [' H ', ' e ', ' l ', ' l ', ' o ', ', ' W ', ' O ', ' r ', ' L ', ' d ', ' \ n ')//
Fast new Long Object
Public class Longdemo { publicstaticvoid main (string[] args) { New Long ("5"); SYSTEM.OUT.PRINTLN (num); System.out.println (Num.longvalue ());} }
Let Javalong = Java.newinstancesync (' Java.lang.Long ', 5)// possibly truncated Long value:5 Console.log (' Possibly truncated Long value:%d ', Javalong)// Original Long value (as a string): 5console.log (' Original Long value (as a string):%s ', javalong.longvalue)
node. JS calls itself to compile the class
Package Com.nearinfinity.nodeJava; Public class MyClass { publicstaticint addnumbers (intint b) { return A + B; }}
Const Java = require (' java ') Java.classpath.push ('./src '= Java.import (' Com.nearinfinity.nodeJava.MyClass '= Myclass.addnumberssync (1, 2= Java.newinstancesync (' Java.lang.Integer ', 2)// Quick Call method inside Java Static class result = Java.callstaticmethodsync (' Com.nearinfinity.nodeJava.MyClass ', ' addnumbers ', Javainteger, 3) console.log (Result)
Conclusion
The above method just takes you to understand the node-java
tip of the iceberg, the node-java
API is very rich, such as,, jvm
, and 实例一个类
调用类静态方法
快速实例化一个对象
so on.
The quickest way to learn is to look at the document, document address: Node-java.
If you like our articles, follow our public number and interact with us.
The actual combat series of node. JS Play Java