Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:50+00:00 2026-06-15T15:48:50+00:00

I have 3 questions regarding using Groovy in java. They are all related together

  • 0

I have 3 questions regarding using Groovy in java. They are all related together so I’m only creating one question here.

1) There are: GroovyClassLoader, GroovyShell, GroovyScriptEngine. But what is the difference between using them?

For example for this code:

static void runWithGroovyShell() throws Exception {
    new GroovyShell().parse(new File("test.groovy")).invokeMethod("hello_world", null);
}

static void runWithGroovyClassLoader() throws Exception {
    Class scriptClass = new GroovyClassLoader().parseClass(new File("test.groovy"));
    Object scriptInstance = scriptClass.newInstance();
    scriptClass.getDeclaredMethod("hello_world", new Class[]{}).invoke(scriptInstance, new Object[]{});
}

static void runWithGroovyScriptEngine() throws Exception {
    Class scriptClass = new GroovyScriptEngine(".").loadScriptByName("test.groovy");
    Object scriptInstance = scriptClass.newInstance();
    scriptClass.getDeclaredMethod("hello_world", new Class[]{}).invoke(scriptInstance, new Object[]{});
}

2) What is the best way to load groovy script so it remains in memory in compiled form, and then I can call a function in that script when I need to.

3) How do I expose my java methods/classes to groovy script so that it can call them when needed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:48:52+00:00Added an answer on June 15, 2026 at 3:48 pm

    Methods 2 and 3 both return the parsed class in return. So you can use a map to keep them in memory once they are parsed and successfully loaded.

    Class scriptClass = new GroovyClassLoader().parseClass(new File("test.groovy"));
    map.put("test.groovy",scriptClass);
    

    UPDATE:

    GroovyObject link to the groovy object docs.

    Also this is possible to cast the object directly as GroovyObject and other java classes are indistinguishable.

    Object aScript = clazz.newInstance();
    MyInterface myObject = (MyInterface) aScript;
    myObject.interfaceMethod();
     //now here you can also cache the object if you want to
    

    Cannot comment on efficiency. But I guess if you keep the loaded classes in memory, one time parsing would not hurt much.

    UPDATE For efficiency:
    You should use GroovyScriptEngine, it uses script caching internally.

    Here is the link: Groovy Script Engine

    Otherwise you can always test it using some performance benchmarks yourself and you would get rough idea. For example: Compiling groovy scripts with all three methods in three different loops and see which performs better. Try using same and different scripts, to see if caching kicks in, in some way.

    UPDATE FOR PASSING PARAMS TO AND FROM SCRIPT
    Binding class will help you sending the params to and from the script.

    Example Link

    // setup binding
    def binding = new Binding()
    binding.a = 1
    binding.setVariable('b', 2)
    binding.c = 3
    println binding.variables
    
    // setup to capture standard out
    def content = new StringWriter()
    binding.out = new PrintWriter(content)
    
    // evaluate the script
    def ret = new GroovyShell(binding).evaluate('''
    def c = 9
    println 'a='+a
    println 'b='+b
    println 'c='+c 
    retVal = a+b+c
    a=3
    b=2
    c=1
    ''')
    
    // validate the values
    assert binding.a == 3
    assert binding.getVariable('b') == 2
    assert binding.c == 3 // binding does NOT apply to def'd variable
    assert binding.retVal == 12 // local def of c applied NOT the binding!
    
    println 'retVal='+binding.retVal
    println binding.variables
    println content.toString()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding using Semaphores HANDLE WINAPI CreateSemaphore(...); Is there anyway I
I have a question regarding animations via javascript in HTML using DOM. In this
I have a question regarding the method i am using for doing Business Rule
I have a question regarding the best practise of handling formated text when using
I have a question regarding the integration of business objects developed using Linq To
I have a question regarding use of FFT. Using function getBand(int i) with Minim
I have a simple question regarding jQuery selectors. I'm using the tablesorter and I
I have seen several questions regarding SocketException when using Android, but none of them
I have few questions regarding Popover segue. 1) In what case one should use
I have two questions regarding component layout using Sencha Touch 2: How can you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.