Question

I'd like to embed a GroovyConsole into an existing java app for design prototyping and debugging purposes.

I'd really like this console to have access to my entire program--mostly this means that any singletons or factories should return the actual object, not create a new one just for the GroovyConsole's world.

Is there any way to do this, or do I have to manually pass all the objects in one by one using setVariable() calls?

Was it helpful?

Solution

This is actually super easy. Most likely, you can just create a new console with the no-arg constructor and it will work out how you want it to. If your application uses multiple classLoaders, just pass in the one you want when you create the Console instance:

def cl = ... //which ClassLoader you'll need depends on your application
def console = new Console(cl)
console.run()

This will create a new Groovy Console and tell it to use whatever class loader you want it to. This does not set any bindings or variables, but it ensures that the classes that the console is using are exactly the same ones the rest of the application is. If you're not doing anything special with the classloaders in your application, a simple call to new Console() will cause it to use the current classloader as a parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top