j4rs icon indicating copy to clipboard operation
j4rs copied to clipboard

use a java library?

Open jianshu93 opened this issue 1 year ago • 1 comments

Hello Team,

I have a java library to use, code will be like this: https://github.com/dynatrace-research/exaloglog-paper/blob/main/java/src/test/java/com/dynatrace/exaloglogpaper/DistinctCountUtilTest.java

How can I use it in j4rs?

Thanks, Jianshu

jianshu93 avatar Feb 24 '24 03:02 jianshu93

You should just create the Jvm specifying the class you want to include in the classpath.

For example, if you have the Java class:

package org.my;

public class Test {
    public void greet(String name) {
        System.out.println("Hello " + name);
    }
}

in a directory /some/location/classes, you can invoke the method greet like:

let jvm: Jvm = JvmBuilder::new().classpath_entry(ClasspathEntry::new("/some/location/classes")).build()?;
let test_instance = jvm.create_instance("org.my.Test", &[])?;
let name = InvocationArg::try_from("Chester")?;
let _ = jvm.invoke(&test_instance, "greet", &[name])?;

astonbitecode avatar Feb 26 '24 07:02 astonbitecode

It's been some time since this is inactive. Closing...

astonbitecode avatar Apr 10 '24 13:04 astonbitecode