What I did was basically follow the OP's answer, but I had beforehand to create the .jar file. To do so, I just prepared my custom class in test.java:
import java.lang.System;
public class Test {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
And then
javac Test.java
jar cf test.jar Test.class
Now you should see the .jar created. From this point, you are able to access it from Javascript:
let java= require('java');
java.classpath.push('test.jar');
let test = java.import("Test");
test.main(null) // should print "Hello, World!"
Answer from testing_22 on Stack OverflowWhat I did was basically follow the OP's answer, but I had beforehand to create the .jar file. To do so, I just prepared my custom class in test.java:
import java.lang.System;
public class Test {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
And then
javac Test.java
jar cf test.jar Test.class
Now you should see the .jar created. From this point, you are able to access it from Javascript:
let java= require('java');
java.classpath.push('test.jar');
let test = java.import("Test");
test.main(null) // should print "Hello, World!"
After a lot of trying, this is what worked for me: (test.jar is in same directory as the other files)
let java= require('java');
java.classpath.push('test.jar');
let mySumTest= java.import("MySumClass");
/*
//this prints out undefined, don't know why
console.log(java.callStaticMethod("MySumClass", "sum", 2, 5, function(err, results){
if(err){
console.error(err);
return;
}
else return results;
}));
*/
var result= java.callStaticMethodSync("MySumClass", "sum", 2, 5)
console.log(result)
error when using the import java class
How to import and use Java method in Node JS
Need guidance on creating Node class (java)? - Stack Overflow
java - Import Node into new Document - Stack Overflow
While implementing the custom LinkedList/Tree, we need Node. Here is demo of creating Node and LinkedList. I have not put in all the logic. Just basic skeleton is here and you can then add more on yourself.

I can give you a quick hint on how to do that:
Class Node{
//these are private class attributes, you need getter and setter to alter them.
private int item;
private Node nextNode;
//this is a constructor with a parameter
public Node(int item)
{
this.item = item;
this.nextNode = null;
}
// a setter for your item
public void setItem(int newItem)
{
this.item = newItem;
}
// this is a getter for your item
public int getItem()
{
return this.item;
}
}
You can create a Node object by calling:
Node newNode = Node(2);
This is not a complete solution for your problem, the two parameter constructor and the last node link are missing, but this should lead you in the correct direction.
I'm trying to write my own implementation of a LinkedList and have come across Nodes (class LinkedList has Node head, Node tail). This is something I have never used before and I'm not sure what I'm doing. To use it, the IDE first prompted me to import org.w3c.dom.Node, why am I importing from what looks like a third party? (not import java.something.something). Also, what is a Node anyway? I read this here:
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
but did not understand the explanation. Thanks.