The condition that node calls Java is the first node's running environment to install Java's JDK
Then you install the Java module in node
NPM Install Java
And then the node code.
Node calls Java's static method:
var java = require ("java");//Introduce Java module Java.classpath.push ("Xx.jar");// Import jar Package Java.classpath.push ("Xx.jar");//import jar package var str2= ' Hellow ';//define variable parameter var list = Java.callstaticmethodsync ("Com.demo.test", "Test", "str1", str2);//Call the Java method, the first parameter is the path where the package is located, the second parameter is the method name, The third and fourth parameters are the parameters of the method, without the///parameter, the third fourth parameter Console.log (list) is not passed;//The return value of the Print method
Node calls a non-static method:
varjava = require ("Java");//Introduction of Java Module Java.classpath.push ("Commons-lang3-3.1.jar");//import jar package Java.classpath.push ("Commons-io.jar");//import jar PackagevarList = Java.newinstancesync ("Java.util.ArrayList")///Load class is JS object Java.newinstance ("Java.util.ArrayList",function(err, list) {//Instance object List.addsync ("Item1");//Call Method List.addsync ("Item2");//Call Method});varArrayList = Java.import (' java.util.ArrayList ')); Get ObjectvarList =NewArrayList ();//Instantiate Object List.addsync (' item1 ');//Call method
Node calls Java