eobermuhlner / jshell-scriptengine

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

Reusing the underlying JShell instance? #3

Closed ChristianCiach closed 5 years ago

ChristianCiach commented 5 years ago

This is more a question than an issue, or maybe even a feature request:

I am essentially looking for a way to call ScriptEngine.eval(...) multiple times while keeping the underlying state of the JShell.

My use case is this: I want to create a ScriptEngine and pre-populate it with imports of classes we are using all the time internally. Then I want to pass the instance of the prepared ScriptEngine to the user, so that he can reference classes in his JShell script without importing them by himself.

eobermuhlner commented 5 years ago

I tried something similar and was not able to keep the underlying state with the JShell API.

With the var keyword introduced with Java 10 you can sometimes avoid the need to import the class.

Maybe with later Java versions the JShell API becomes more powerful but with the current Java <= 12 this is unfortunately not possible.

eobermuhlner commented 5 years ago

What you can do in your project is to always prefix the script with your hardcoded imports before you call ScriptEngine.eval(...).

This would look the same to the end user.