jimm2
jimm2

Reputation: 1

Run methods from Java Classes using J2V8

I'm in need of guidance for executing methods from Java classes using J2V8. While I can register a JS method with a Java callback, there are a lot of methods to define and thats is boring. Your expertise and any code examples would be greatly appreciated.

Example of what I am trying to do

package com.example;

public class Main {
 public static record Cat(String name, int age) {
  public void greet() {
   System.out.println("hello " + name + "!");
  }
 }

 public static int square(int number) {
  return number*number;
 }

 public static void main(String[] args) {
  V8 v8 = V8.createV8Runtime();
  v8.executeVoidScript("<javascript code below>");
 }
}

var myCat = new com.example.Main.Cat("bob", 2);
myCat.greet() // hello bob!

java.lang.System.out.println(com.example.Main.square(10));

Or something like that.

Thank you!

I also tried to use the import keyword in V8 but that did not work.

Upvotes: 0

Views: 93

Answers (0)

Related Questions