Call Java from Rust with j4rs - get error when run with cargo run

I want to call some java packages from Rust with j4rs crate.
I download all the dependent jars and load them with JVM::deploy_artifact.
After I build the project with cargo build, I can directly run the binary without any error.
But when I run the project with cargo run, I will get the following error from java.

Exception in thread "main" java.lang.NoClassDefFoundError: org/astonbitecode/j4rs/api/instantiation/NativeInstantiationImpl
Caused by: java.lang.ClassNotFoundException: org.astonbitecode.j4rs.api.instantiation.NativeInstantiationImpl
	at java.net.URLClassLoader.findClass(URLClassLoader.java:407)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
thread 'main' panicked at 'Jvm should be initiated: JavaError("An Exception was thrown by Java while creating global ref... Please check the logs or the console.")', src\main.rs:29:18

Does anybody know why?

What Java version are you using? I remember having some issues because mine was too old.

Are you on Windows? If so, I believe you should use j4rs v0.15.3 because in earlier versions there was an issue with UNC handling. The classpath was not populated correctly when running with cargo...

Thanks. I am on Windows and I am using jdk 1.8. :joy: I believe j4rs v0.15.3 uses logback v1.4.x compiled by Java major version 55 which is not compatible with jdk 1.8. I guess I have to tolerate this annoying bug right now.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.