Could not successfully 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 mentioned before!
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.
One solution is to create Vendor.groovy and Address.groovy containing the respective class definitions.
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:
- Follow directory structure of your src files as @Andrey Adamovich suggested. So, both your
.groovy
files should reside inbusiness
folder - Open
cmd
, navigate to the parent ofbusiness
folder, e.g.src/main/groovy
for my projeсt (I have structuresrc/main/groovy/business
). - Execute the script with command
groovy business/importTesting.groovy
精彩评论