pub struct JvmBuilder<'a> { /* private fields */ }Expand description
A builder for Jvm
Implementations§
Source§impl<'a> JvmBuilder<'a>
impl<'a> JvmBuilder<'a>
Sourcepub fn new<'b>() -> JvmBuilder<'b>
pub fn new<'b>() -> JvmBuilder<'b>
Creates a new JvmBuilder.
Sourcepub fn classpath_entry(
&'a mut self,
cp_entry: ClasspathEntry<'a>,
) -> &'a mut JvmBuilder<'a>
pub fn classpath_entry( &'a mut self, cp_entry: ClasspathEntry<'a>, ) -> &'a mut JvmBuilder<'a>
Adds a classpath entry.
Sourcepub fn classpath_entries(
&'a mut self,
cp_entries: Vec<ClasspathEntry<'a>>,
) -> &'a mut JvmBuilder<'a>
pub fn classpath_entries( &'a mut self, cp_entries: Vec<ClasspathEntry<'a>>, ) -> &'a mut JvmBuilder<'a>
Adds classpath entries.
Sourcepub fn java_opt(&'a mut self, opt: JavaOpt<'a>) -> &'a mut JvmBuilder<'a>
pub fn java_opt(&'a mut self, opt: JavaOpt<'a>) -> &'a mut JvmBuilder<'a>
Adds a Java option.
Sourcepub fn java_opts(&'a mut self, opts: Vec<JavaOpt<'a>>) -> &'a mut JvmBuilder<'a>
pub fn java_opts(&'a mut self, opts: Vec<JavaOpt<'a>>) -> &'a mut JvmBuilder<'a>
Adds Java options.
Sourcepub fn with_no_implicit_classpath(&'a mut self) -> &'a mut JvmBuilder<'a>
pub fn with_no_implicit_classpath(&'a mut self) -> &'a mut JvmBuilder<'a>
By default, the created Jvms include an implicit classpath entry that includes the j4rs jar.
When with_no_implicit_classpath() is called, this classpath will not be added to the Jvm.
Sourcepub fn detach_thread_on_drop(
&'a mut self,
detach_thread_on_drop: bool,
) -> &'a mut JvmBuilder<'a>
pub fn detach_thread_on_drop( &'a mut self, detach_thread_on_drop: bool, ) -> &'a mut JvmBuilder<'a>
When a Jvm goes out of scope and is being dropped, its current thread is being detached from the Java VM.
A Jvm that is created with detach_thread_on_drop(false) will not detach the thread when being dropped.
This is useful when in the Java world a native method is called and in the native code someone needs to create a j4rs Jvm. If that Jvm detaches its current thread when being dropped, there will be problems for the Java world code to continue executing.
Sourcepub fn with_native_lib_name(
&'a mut self,
lib_name: &str,
) -> &'a mut JvmBuilder<'a>
pub fn with_native_lib_name( &'a mut self, lib_name: &str, ) -> &'a mut JvmBuilder<'a>
In the case that the j4rs is statically linked to some other library, the Java world (j4rs.jar) needs to load that library instead of the default one.
This function defines the native library name to load.
Sourcepub fn skip_setting_native_lib(&'a mut self) -> &'a mut JvmBuilder<'a>
pub fn skip_setting_native_lib(&'a mut self) -> &'a mut JvmBuilder<'a>
Configures the builder not to instruct the Java world j4rs code to load the native library. (most probably because it is already loaded)
Sourcepub fn with_base_path(&'a mut self, base_path: &str) -> &'a mut JvmBuilder<'a>
pub fn with_base_path(&'a mut self, base_path: &str) -> &'a mut JvmBuilder<'a>
Defines the location of the jassets and deps directory. The jassets contains the j4rs jar and the deps the j4rs dynamic library.
Sourcepub fn with_maven_settings(
&'a mut self,
maven_settings: MavenSettings,
) -> &'a mut JvmBuilder<'a>
pub fn with_maven_settings( &'a mut self, maven_settings: MavenSettings, ) -> &'a mut JvmBuilder<'a>
Defines the maven settings to use for provisioning maven artifacts.
Sourcepub fn with_javafx_support(&'a mut self) -> &'a mut JvmBuilder<'a>
pub fn with_javafx_support(&'a mut self) -> &'a mut JvmBuilder<'a>
Adds JavaFX support to the created JVM
Sourcepub fn with_java_vm(
&'a mut self,
java_vm: *mut JavaVM,
) -> &'a mut JvmBuilder<'a>
pub fn with_java_vm( &'a mut self, java_vm: *mut JavaVM, ) -> &'a mut JvmBuilder<'a>
Create the j4rs Jvm using an already created jni JavaVM.
Useful for Android apps, where the JVM is automatically created.
Sourcepub fn with_classloader_of_activity(
&'a mut self,
jobject_within_valid_classloader: jobject,
) -> &'a mut JvmBuilder<'a>
pub fn with_classloader_of_activity( &'a mut self, jobject_within_valid_classloader: jobject, ) -> &'a mut JvmBuilder<'a>
Instructs j4rs to use the classloader associated to the specified jobject when searching for classes.
Useful for pure native Android apps.
As described in Android documentation, the native libraries that spawn new threads cannot locate and use Java classes correctly.
From the docs:
You can get into trouble if you create a thread yourself (perhaps by calling pthread_create and then attaching it with AttachCurrentThread). Now there are no stack frames from your application. If you call FindClass from this thread, the JavaVM will start in the “system” class loader instead of the one associated with your application, so attempts to find app-specific classes will fail.
Even if it is proposed to load and cache classes during JNI_OnLoad (j4rs documentation about it here)
JNI_OnLoad is not called on pure native apps. One solution to find the app-specific classes in this case, is to use the classloader
of the Activity to find and use classes. j4rs will use the jobject passed here (the jobject of the Activity) to get the
proper classloader and use it when needed.
#[cfg(target_os = “android”)]
Sourcepub fn with_default_classloader(&'a mut self) -> &'a mut JvmBuilder<'a>
pub fn with_default_classloader(&'a mut self) -> &'a mut JvmBuilder<'a>
j4rs uses a custom ClassLoader (namely the J4rsClassLoader),
that allows adding jars to the classpath during runtime, after the underlying JVM is initialized.
This function instructs the builder not to use this custom classloader, but use the default one.
Please note that the J4rsClassLoader needs Java 9 or higher. If you use an older Java version,
you must call this function in order for j4rs to work.
If not, you will get exceptions like the following:
java.lang.NoSuchMethodError: java.net.URLClassLoader.`<init>`(Ljava/lang/String;[Ljava/net/URL;Ljava/lang/ClassLoader;)V
at org.astonbitecode.j4rs.api.deploy.J4rsClassLoader.`<init>`(J4rsClassLoader.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2204)
at java.lang.SystemClassLoaderAction.run(ClassLoader.java:2188)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1449)
at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429)Sourcepub fn already_initialized() -> Result<Jvm>
pub fn already_initialized() -> Result<Jvm>
Creates a Jvm, similar with an already created j4rs Jvm.
Note: The already created Jvm is a j4rs Jvm, not a Java VM.