jisolate
                                
                                 jisolate copied to clipboard
                                
                                    jisolate copied to clipboard
                            
                            
                            
                        Java Code Isolation
Jisolate
Jisolate is an API for isolating Java classes using either classloader or VM isolation.
Why Isolation?
Isolates are useful to load the same class multiple times, typically using different configurations. For example, some frameworks are configured using static singletons. For tests, it is often useful to start multiple instances of these frameworks, each with its own configuration.
Classloader Isolation
Classloader isolation uses a child-first classloader combined with thread-local system properties to ensure that non-JDK classes can be loaded multiple times in the same JVM.
Classloader isolation is the isolation approach of choice as it is the most performant approach of ensuring isolation and allows for an easy way to communicate results from the isolated code back to its invoker.
Jisolate.classLoaderIsolation()
    .withIsolatableClass(IsolatedClass.class)
    .withIsolatableArguments("foo")
    .isolate();
VM Isolation
VM isolation spawns a child VM process. This provides even better isolation than the classloader approach, but it takes slightly longer to setup.
Jisolate.jvmIsolation()
    .withMainClass(IsolatedClass.class)
    .withMainClassArguments("foo")
    .isolate();
Lifecycle control
VM isolation also allows for forcefully terminating the isolated JVM process when it's no longer needed:
try (JvmIsolate isolate = Jisolate.jvmIsolation()
    .withMainClass(IsolatedClass.class)
    .withMainClassArguments("foo")
    .isolate()) {
    
    // The isolated JVM is automatically terminated on leaving this block    
}
JSR-121
Jisolate is not an implementation of JSR-121, the Application Isolation API Specification. Instead, it is a light-weight and pragmatic approach of providing a best effort isolation of Java classes that is useful for many scenarios.