My assumption here is that you have java code and the result you want to display is on nodejs.
While at this moment calling data from the API is suggested, also there is one more option we can use to solve your problem. This is a good case of polyglot programming. I have used graalvm.
Installation via sdkman
GraalVM
sdk install java 21.1.0.r11-grl
NodeJS
gu install nodejs
Both Main Projects are located here https://gitlab.com/graalvm-java-to-nodejs
- Java Project (has method which return a list)
- NodeJS Project(loads Java Class in NodeJS and call method on class reference to get the list)
Add any code to java library in my case I have a class which just return list of Point as given below:
public class GraphData {
public List<Point> getPoints() {
return List.of(new Point(1, 1), new Point(3, 5));
}
}
where Point
is a POJO class to hold (x, y) value.
Clone this java project https://gitlab.com/graalvm-java-to-nodejs/graalvm-simple-java
and execute ./gradlew clean build
this should give you a executable jar which can be executed java -jar file.jar command.
Now, clone https://gitlab.com/graalvm-java-to-nodejs
and install dependencies npm install
then execute
node --jvm --vm.cp=/home/ashish/IdeaProjects/graavlvm/java-lib-gvm/build/libs/java-lib-gvm-1.0-SNAPSHOT.jar bin/www
Relevant code which interacts with java is as below:
var GraphDataJavaRef = Java.type('in.silentsudo.GraphData');
var graphData = new GraphDataJavaRef();
var data = graphData.getPoints();
in.silentsudo.GraphData
class is loaded from the jar file which is provided to node program with argument named --jvm --vm.cp path/to/file.jar
Once you navigate to localhost:3000, you should see
Express Tutorial
Welcome to Express Tutorial
Response from Java class
[Point{x=1, y=1}, Point{x=3, y=5}]