Ant's Ant's - 1 year ago 134
Groovy Question

Could not successfuly Import class in Groovy?

I created a two class named Vendor and Address in one file called business.groovy(that is I created package using the name business)
And in the same folder I created another groovy code that imports the business package and make use of Vendor class attributes(Vendor is class in business package). But when I try to create a object for Vendor in that groovy script, it throwing error stating that:

/home/Anto/Groovy/pakg/Imports.groovy: 2: unable to resolve class Vendor
@ line 2, column 13.
def canoo = new Vendor()

What do I have to do? Where did I go wrong?

These are the files I created:

package business
class Vendor {
public String name
public String product
public Address address = new Address()
class Address {
public String street, town, state
public int zip


import business.*
def canoo = new Vendor() = 'Canoo Engineering AG'
canoo.product = 'UltraLightClient (ULC)'

And when I try to execute the importTesting.groovy file by using groovy importTesting command, I get the error as I metioned before!

Answer Source

I would suggest you to either use some build tool like Ant or Gradle, or IDE like IntelliJ IDEA to control your classpath/compilation/runtime needs.

EDIT: It should be like that:


Your compiled classes should also resemble the same directory structure:


Then your baseDir should be added to the CLASSPATH.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download