Groovy – unable to resolve class

The Problem

I have a groovy script that I’m writing and I’m trying to [link to | include | reference] another groovy “library” that I’ve [written | downloaded].

I import the “library” at the top of my script:

import com.mycompany.SomeGroovyLib

But, when trying to run my script, it fails with an error:

W:\path_to_file\script.groovy: 9: unable to resolve class com.mycompany.SomeGroovyLib
 @ line 9, column 1.
   import com.mycompany.SomeGroovyLib
   ^

error

I’m using IntelliJ IDEA 2017.2.6 on Windows with groovy 2.4.6 but the same happens on earlier versions.

The solution

This is a simple one.

From the “File” menu [1], choose the “Project Structure…” menu item [2]:

project_structure

Then, from the “Project Structure” dialog, choose the “Modules” item in the left pane [1], then the “Dependencies” tab [2]. Now choose the + symbol on the right hand side of the dialog [3] and choose the “1 JARs or directories…” option [4].

choose_dir

Now, navigate to the path of your groovy library such that the path of your import statement …:

import

…can be found immediately within the folder you select:

folder_choose

Finally, you will be prompted to choose what type of thing you’re linking to and you should select “Classes”:

choose_classes

Now you can “ok” your way back through the 2 dialogs and your project should build.

🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s