I need to add the "-XstartOnFirstThread" option to the JavaVM arguments in order to display the GUI on MacOs (Without this option, the application starts without errors, but does not display the GUI). On Linux and Windows without this option everything works fine. It is only needed for MacOs. When I add it to the list of options I get the error:
Unrecognized option: -XstartOnFirstThread
thread 'main' panicked at src/main.rs:39:6:
Failed to create JavaVM: Create(JniCall(Unknown))
How can I add it to the JVM? After all, without it, my application does not work correctly on MacOs (the application window is not created)
The code I use to initialize and start the JVM:
let jvm_args = InitArgsBuilder::new()
.version(JNIVersion::V8)
.option("-XstartOnFirstThread") // This option need for run GUI on MacOs
//Classpath with natives skipped
.build()
.unwrap();
let jvm = JavaVM::new(jvm_args)
.expect("Failed to create JavaVM");
let mut env = jvm
.attach_current_thread()
.expect("Failed to attach JNIEnv");
//Java class find and invoke static method Main skipped.
I spent several hours searching for answers on google and reading the documentation, but I still couldn’t find an answer, has anyone encountered this problem or have any ideas?
After some investigation, it turns out that
-XstartOnFirstThreadis implemented in something called "libjli", not libjvm. Thejavaexecutable links against libjli, whereas rust-jni uses libjvm as it normally should.I managed to hack together the following code that uses the JLI_Launch function to start a Java interpreter. It launches the JVM on the main thread as you requested. You can verify this by setting
_JAVA_LAUNCHER_DEBUG=1.