1

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
Bill K
  • 62,186
  • 18
  • 105
  • 157
  • You can get useful input from [here](http://groovy.codehaus.org/Embedding+a+Groovy+Console+in+a+Java+Server+Application). – dmahapatro May 30 '13 at 01:46
  • If I were using spring that would be absolutely perfect. It's really close, but since I'm not using spring I don't have an application context I can bind. – Bill K May 30 '13 at 20:51

1 Answers1

2

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.

allTwentyQuestions
  • 1,160
  • 7
  • 11
  • your last statement doesn't seem right. I've started a new console with no parameters and haven't been able to access objects in the rest of the app without explicitly sharing them, but I'll give it another try. I'm guessing the rest is correct and passing in the classloader will work. – Bill K Feb 07 '14 at 21:16
  • Yeah, just checked it. If you pass in getClass().getClassLoader() you get exactly what I wanted, if you do not it must create it's own class loader. Thanks for the help! – Bill K Feb 07 '14 at 21:28