eobermuhlner / jshell-scriptengine

JShell script engine for Java (JSR-223 compatible)
MIT License
32 stars 8 forks source link

How to use this with Modules? #8

Open sproket opened 11 months ago

sproket commented 11 months ago

Trying to get a small sample to work with modules.

module TestJShell {
    requires java.scripting;
    requires java.logging;
    requires jdk.jshell; // required?

    requires ch.obermuhlner.scriptengine.jshell;

    opens org.example to ch.obermuhlner.scriptengine.jshell;
    exports org.example to ch.obermuhlner.scriptengine.jshell;
}

java source

package org.example;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.logging.Logger;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("jshell");
        Logger logger = Logger.getLogger("testjshell");
        Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.put("log", logger);

//        engine.put("log", logger);
        String text = """
                System.out.println("HELLO WORLD");
                log.info("Hello world.");
                """;

        engine.eval(text);

    }
}

and I get:

Exception in thread "main" javax.script.ScriptException: package ch.obermuhlner.scriptengine.jshell does not exist
Snippet:VariableKey(log)#1-java.util.logging.Logger log = (java.util.logging.Logger) ch.obermuhlner.scriptengine.jshell.VariablesTransfer.getVariableValue("log");