IntelliJ IDEA 2023.3 Help

Getting started with Groovy and Java 11 project

Since JDK 11 requirements have changed for Groovy, you need to perform some additional steps to successfully run the Groovy code in IntelliJ IDEA.

In this tutorial, you'll create a Groovy project with JDK 11, add simple code, and run a Groovy script.

Step 1. Create a project

Let's create a Groovy project with Java.

Create a new Groovy project

  1. On the welcome screen, click New Project.

    To the options on the right, IntelliJ IDEA automatically adds a project SDK (JDK). In our tutorial we use the open JDK 11 version.

    The Groovy library should be downloaded on your machine. IntelliJ IDEA expects the standard Groovy SDK layout which is provided with the official distributions available at http://groovy-lang.org/download.html. Download the SDK, unpack it into any directory, and specify this directory as the library home. In this tutorial, it is Groovy 2.5.6.

    Create Groovy project

    Click Next.

  2. On the next page of the wizard, let's specify the name (GroovyDemo11) and location of the project. Click Finish.

Step 2. Add and Run code

Let's create a Groovy script.

Add Groovy script

  1. In the Project tool window, right-click the src directory and select New | Groovy Script. IntelliJ IDEA opens the file in the editor.

  2. Type the following code:

    println("Hello, Groovy!")
  3. Press Ctrl+Shift+F10 to run the script.

    Run Groovy script

    When you run this code, you will get the internal error:

    Run tool window: Groovy internal error

    So, let's tweak the run configuration and try to solve our problem.

Step 3. Edit the run configuration

Add the following code to the VM options in the run configuration dialog:

Modify the run configuration

  1. In the Project tool window, right-click the created Groovy script file (hello.groovy) and select More Run/Debug | Modify Run Configuration.

  2. In the dialog that opens, let's add the following code to the VM options field:

    --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED
  3. Click OK to save the changes.

    Groovy run configuration dialog

    However, if you rerun the script now, you will still get the same internal error as before.

Step 4. Add dependencies and rerun the script

Since you use Groovy 2.5.6 and Java 11 in the project, you need to add the extra JAXB Jars that this Groovy version comes with to the project as dependencies.

Add JAXB dependency

  1. Press Ctrl+Alt+Shift+S to open the Project Structure dialog.

  2. In the dialog that opens, select the Modules option from the Project Settings list.

  3. From the option on the right, select the Dependencies tab.

  4. Click on the bottom of the dialog and select JARs or Directories.

    Project Structure dialog / Module page
  5. In the dialog that opens, locate extras-jaxb directory that the Groovy SDK comes with and add it as a dependency.

    Finder: extras_jaxb
  6. Click OK to save the changes.

    Now when you run the script, you will get the expected output in the Run tool window.

    Run tool window: Hello, Groovy!
Last modified: 19 March 2024