Setting Up Sublime Text to Run Groovy Scripts

Groovy is a powerful scripting language that builds on Java, making it easier to write concise and expressive scripts. Sublime Text, a highly customizable text editor, can be configured to execute Groovy scripts, streamlining your workflow. This guide will walk you through the steps to set up Sublime Text to run Groovy scripts on your system.

Prerequisites

Before proceeding, ensure the following:

  1. Download and install Groovy from the official website.
  2. Download Sublime Text from the official website.


Step 1: Create a Build System for Groovy

A build system in Sublime Text allows you to execute scripts directly from the editor. Follow these steps:

  1. Open Sublime Text.

  2. Navigate to Tools > Build System > New Build System.

  3. Replace the default content with the following configuration:

    {
        "cmd": ["C:\\groovy-2.4.12\\bin\\groovy.bat","$file"],
        "selector": "source.groovy"
    }
    
    
    Replace path_to_groovy with the actual installation path of Groovy on your system. For example, C:\\Groovy\\bin.
  4. Save the file as groovy.sublime-build in the default location suggested by Sublime Text.


Step 2: Configure for macOS or Linux

If you are using macOS or Linux, modify the cmd field:

{
    "cmd": ["/usr/local/bin/groovy", "$file"],
    "selector": "source.groovy"
}

Replace /usr/local/bin/groovy with the appropriate path to your Groovy executable.


Step 3: Write and Save a Groovy Script

  1. Open a new file in Sublime Text.
  2. Write a simple Groovy script, such as:
      
      println "Hello, Groovy!"
      
      
  3. Save the file with a .groovy extension.


Step 4: Run the Script

  1. Open your script in Sublime Text.
  2. Select Tools > Build System > Groovy.
  3. Press Ctrl+B (or Cmd+B on macOS) to execute the script.
  4. The output will appear in the build output panel at the bottom of Sublime Text.

Troubleshooting Tips

  • Error: ‘groovy.bat’ not found: Ensure the correct path to groovy.bat is specified in the build system file.
  • Permission Issues: Run Sublime Text as an administrator (Windows) or ensure the necessary permissions (Linux/macOS).
  • Groovy Not Recognized: Verify your Groovy installation by running groovy -v in your terminal or command prompt.