Ant's Ant's - 3 days ago 6
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:
business.groovy

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
}


impotTesting.groovy

import business.*
def canoo = new Vendor()
canoo.name = '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

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:

baseDir/business/business.groovy
baseDir/impotTesting.groovy

Your compiled classes should also resemble the same directory structure:

baseDir/business/business*.class  
baseDir/impotTesting*.class

Then your baseDir should be added to the CLASSPATH.

Comments