JvmClassLoading extension

Class loaders, and classes defined from class-file bytes.

Some things are only possible from Java — holding a monitor on another thread, a thread pool, a caller-sensitive JDK API — and shipping a jar for a twenty-line helper is out of proportion. Instead, compile the helper with dart run java_interop:embed_java, which writes its class files into a Dart file, and define them at runtime:

final loader = jvm.newClassLoader();
final classes = jvm.defineClasses(loader, myHelperClasses);
final helper = classes['com.example.MyHelper']!;

This is how the binding ships its own proxy handler and JavaExecutor.

on

Methods

defineClass(JavaObject loader, String name, Uint8List bytes) → JavaClass

Available on Jvm, provided by the JvmClassLoading extension

Defines bytes — a class file — as name in loader, returning the class as a global reference the caller releases.
defineClasses(JavaObject loader, Map<String, Uint8List> classes) → Map<String, JavaClass>

Available on Jvm, provided by the JvmClassLoading extension

Defines every class of classes in loader, and returns them by name, as global references the caller releases.
newClassLoader({JavaObject? parent}) → JavaObject

Available on Jvm, provided by the JvmClassLoading extension

A new, empty class loader — a java.net.URLClassLoader with no URLs — as a global reference the caller releases.