2

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 mentioned before!

1
  • What do you use to compile and run groovy scripts? Some IDE or build tool? Mar 7, 2011 at 14:40

3 Answers 3

3

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.

11
  • then you need to modify your CLASSPATH variable to include the folder where compiled Groovy files are. Mar 7, 2011 at 14:45
  • ya my groovy Files are getting compiled and running using the commands(groovyc and groovy) but the problem is in packages :(
    – Ant's
    Mar 7, 2011 at 14:46
  • do you define the package in the start of Groovy file? Mar 7, 2011 at 14:47
  • do your Groovy files reside in some folder structure? what is the value of your CLASSPATH variable? Mar 7, 2011 at 14:48
  • ya i do :! and one more question for the pakage definition file, i have to use what extension? Whether it is needed to compile that too, before using it anywhere in Import statements?
    – Ant's
    Mar 7, 2011 at 14:49
0

One solution is to create Vendor.groovy and Address.groovy containing the respective class definitions.

0

I've just faced similar issue while learning Groovy. In order to run your example from command line using groovy command (tested with Groovy 2.4.12), you should:

  1. Follow directory structure of your src files as @Andrey Adamovich suggested. So, both your .groovy files should reside in business folder
  2. Open cmd, navigate to the parent of business folder, e.g. src/main/groovy for my projeсt (I have structure src/main/groovy/business).
  3. Execute the script with command groovy business/importTesting.groovy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.