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 522883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:24:00+00:00 2026-05-13T08:24:00+00:00

Well, it seems a simple task but I didn’t manage to make it run.

  • 0

Well, it seems a simple task but I didn’t manage to make it run.

I have a groovy script that runs fine under Windows Vista when calling from prompt:

> cd MY_GAILS_PROJECT_DIR
> groovy cp src/groovy scripts/myscript.groovy

Now, I want to execute this script (and passing to it some input arguments) through my my Maintenance Service Class (called from a controller) as below,

class MaintenanceService {
  def executeMyScript() {
    "groovy cp src/groovy scripts/myscript.groovy".execute()
  }
}

It does not work at all! I don’t even manage to have the execute() method recognizing any command (like "cd .".execute()) throwing exception:

Error 500: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified

1- How can I execute a groovy script from my grails application?

2- What are the best practices here? For instance, should I use the QuartzPlugin and then the triggerNow method for executing a script? should I use a Gant Task? If yes, how to do it?

Thank you.

  • 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-05-13T08:24:00+00:00Added an answer on May 13, 2026 at 8:24 am

    If you don’t mind your script running asynchronously (in a separate process to the service method), the following should work assuming groovy is on your PATH variable:

    def cmd = ['groovy.bat', 'cp', 'src/groovy scripts/myscript.groovy']
    cmd.execute()
    

    If you want to view the output of the process in the application console, you should try something like this instead

    // Helper class for redirecting output of process
    class StreamPrinter extends Thread {
        InputStream inputStream
    
        StreamPrinter(InputStream is) {
            this.inputStream = is
        }
    
        public void run() {
            new BufferedReader(new InputStreamReader(inputStream)).withReader {reader ->
                String line
                while ((line = reader.readLine()) != null) {
                    println(line)
                }
            }
        }
    }
    
    // Execute the script
    def cmd = ['groovy', 'cp', 'src/groovy scripts/myscript.groovy']
    Process executingProcess = cmd.execute()
    
    // Read process output and print on console
    def errorStreamPrinter = new StreamPrinter(executingProcess.err)
    def outputStreamPrinter = new StreamPrinter(executingProcess.in)
    [errorStreamPrinter, outputStreamPrinter]*.start()
    

    Update:
    In response to your comment below, try the following (which assumes you’re on Windows):

    1: Create the file C:\tmp\foo.groovy. The content of this file should be simply:

    println 'it works!'
    

    2: In the groovy console, run the following:

    cmd = ['groovy.bat', 'C:\\tmp\\foo.groovy']
    cmd.execute().text
    

    3: You should see the result of the script (the text ‘it works!’) shown in the Groovy console

    If you can’t get this simple example working, there’s something wrong with your environment, e.g. ‘groovy.bat’ is not on your PATH. If you can get this example working, then you should be able to work forward from it to achieve your objective.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, it seems simple enough, but I can't find a way to add a
This seems like such a simple task, but I'm having a hard time finding
Well, this one seems quite simple, and it is. All you have to do
It seems to me that it would work perfectly well to do tail-recursion optimization
Well... simple question, right? But with no so simple answers. In firefox i use
I have a mystery.html page that loads some javascript (as well as mootools). I
I have a VERY simple C# Winforms project that I made using Visual Studio
It seems I don't understand javascript callbacks quite as well as I thought. In
Well, this is my first post here and really enjoying the site. I have
I created a simple MySQLi class for a project I'm working on to make

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.