Sharing dynamically loaded classes with JShell instance

后端 未结 3 683
不知归路
不知归路 2021-01-31 18:45

Please view the edits below

I\'m trying to create a JShell instance that gives me access to, and lets me interact with objects in the <

3条回答
  •  庸人自扰
    2021-01-31 19:01

    Now, there is better and easier solution:

    package ur.pkg;
    
    import jdk.jshell.JShell;
    import jdk.jshell.execution.LocalExecutionControlProvider;
    
    public class TestShell {
        public static int testValue = 5;
    
        public static void main(String[] args) {
            JShell shell = JShell.builder().executionEngine(new LocalExecutionControlProvider(), null).build();
            TestShell.testValue++;
            System.out.println(TestShell.testValue);
            shell.eval("ur.pkg.TestShell.testValue++;").forEach(p -> {
                System.out.println(p.value());
            });
            System.out.println(TestShell.testValue);
    
        }
    
    }
    
    

    Default execution engine is JDI, but u can switch it to local or own.

提交回复
热议问题