0

I need to use the Java method getter of this class:

public class GetAyudas {
    public static String[] getter(String correo){
        String[] res;
        if(correo == "[email protected]"){
            return res = new String[]{"Sergio","Durán","Vega"};
        }
        if(correo == "[email protected]"){
            return res = new String[]{"Ramón","Y","Cajal"};
        }
        else{
            return res = new String[]{"Nada","De","Nada"};
        }
    }
    public static void main(String[] args) {
        System.out.println(getter(args[args.length-1]).toString());
    }
}

in Node JS, so I thought that I could call the main method with the argument and save the returned array in the standard output to use it in Node JS following the next code:

const spawn = require("child_process").spawn;

function getAyudas(email) {
  let args = [];
  args.push("GetAyudas");
  args.push(email);

  let worker = spawn("java", args);
  worker.stdout.on("data", function (data) {
    return data;
  });
}

let res = getAyudas("[email protected]");
console.log(res);
console.log(typeof res);

But it doesn't work: res is undefined. How can I call and use getter in a Node JS document ?

7
  • My most encouraging attemp is the one that uses stdout Commented Feb 16, 2023 at 23:09
  • maybe put the console.log(res) instead of the return data (so you have console.log(data))? You cannot return out of the outer function from an inner function. Commented Feb 16, 2023 at 23:22
  • In addition, make sure the Java program works on its own, first. It currently contains multiple errors. In Java, comparing strings with == is incorrect. Instead, use the equals method. Also, calling toString on an array does not provide useful output. Use Arrays.toString(getter(args[args.length-1])) Commented Feb 16, 2023 at 23:57
  • You need a Java program that you can run with spawn that can accept communication from nodejs either in stdin, as an http request or on the command line or some other way and then can communicate back a result, either in stdout, as an http response or via a file or some other mechanism. You can't directly call a java function from nodejs. Commented Feb 16, 2023 at 23:57
  • Do you have the compiled java class GetAyudas.class available in the classpath? Commented Feb 17, 2023 at 0:38

1 Answer 1

1

To better way to use 'node-java' package for inoking the java methods instead of node child process. 'node-java' package allows you to invoke java methods and use java classes within node js application.

For example: First create the java file with name TestJava.java.

public class TestJava {
    public String helloJava(String name) {
        return "Test, " + name + "!";
    }
}

Then compile the java file to generate the .class file. After the generating the class file then create the node.js script with name callJavaMethod.js.

// callJavaMethod.js
const java = require('java');

// Add the current directory to the classpath
java.classpath.push('.');

// Import the Java class
java.import('TestJava');

// Create an instance of the Java class
const testJava= new java.newInstanceSync('TestJava');

// Call the Java method
const result = testJava.helloJava('Java');

// Print the result
console.log(result);

after that you run the node js script and you will get the following answer

Test Java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.