import java.io.Console;
import jdk.jshell.*;
import jdk.jshell.execution.LocalExecutionControlProvider;
class Example {
     public static void main(String[] args) {
         Console console = System.console();

         // Using JShell.create() from the example works correctly, independent
         // of OpenJDK 17 or 21.
         // try (JShell js = JShell.create()) {

         // We use a multiple local execution environments in our own process space,
         // so JShell.create() is not sufficient.
         LocalExecutionControlProvider lecp = new
LocalExecutionControlProvider();
         try (JShell js = JShell.builder().executionEngine(lecp,
null).build()) {
             js.eval("public interface TestInterface { public void bar(); }");
             js.eval("public class TestClass { public void foo() { TestInterface test; if (System.currentTimeMillis() % 2 == 0) { test = new TestInterfaceImpl1(); } else { test = new TestInterfaceImpl2(); } test.bar(); } private class TestInterfaceImpl1 implements TestInterface { public void bar() { System.out.println(\"TestInterfaceImpl1\"); } } private class TestInterfaceImpl2 implements TestInterface { public void bar() { System.out.println(\"TestInterfaceImpl2\"); }} } ");
             js.eval("TestClass t = new TestClass();");
             js.eval("t.foo();");
         }
         System.out.println("\nGoodbye");
     }
}