By Node invoking the JAR Class
In this article we will see how we can invoke the JAVA classes via Nodejs
Methods
- After Searching about how can we invoke the java classes or SDK via JavaScript i got One NPM Library that is used to invoke Java Classes via Nodejs. Lightweight cross-platform JavaScript module to easily call java commands from Node.js sources.
1const JavaCaller = require('java-caller'); 2const java = new JavaCaller(JAVA_CALLER_OPTIONS); 3const {status, stdout, stderr} = java.run(JAVA_ARGUMENTS,JAVA_CALLER_RUN_OPTIONS);
This is the one Syntax of the Library where we can run classes via java.run function given by the library lets take an example to understand how it works.
Call a class located in classpath
1const java = new JavaCaller({ 2 classPath: 'test/java/dist', 3 mainClass: 'com.nvuillam.javacaller.JavaCallerTester' 4}); 5const { status, stdout, stderr } = await java.run();
Where,
classPath : If jar parameter is not set, classpath to use Use :
as separator (it will be converted if runned on Windows), or use a string array.
mainClass : If classPath set, main class to call
More of Examples Here
Internally the Library Uses node spawn method to perform the call , spawn is nodejs inbuilt function of Child Process Module, The
child_process
module enables us to access Operating System functionalities by running any system command inside a, well, child process.