无法在Groovy中成功导入类? [英] Could not successfully Import class in Groovy?

查看:422
本文介绍了无法在Groovy中成功导入类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为business.groovy的文件中创建了名为供应商地址的两个类(即,我使用名称 business
在同一个文件夹中,我创建了另一个groovy代码,用于导入业务包并使用Vendor类属性(Vendor是业务包中的类)。但是,当我尝试在该groovy脚本中为供应商创建对象时,它会抛出错误,指出:
$ b


/ home / Anto / Groovy / pakg / Imports.groovy:2:无法解决类供应商
@第2行第13列。
def canoo = new Vendor()


我需要做什么?

以下是我创建的文件:
business.groovy

 包业务
类供应商{
公共字符串名称
公共字符串产品
公共地址=新地址( )
}
class地址{
public String street,town,state
public int zip
}

impotTesting.groovy

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

当我尝试使用 groovy importTesting 命令执行 importTesting.groovy 文件时,我得到了前面提到的错误!

解决方案

我建议您使用一些构建工具,如 Ant Gradle 或IDE,如 IntelliJ IDEA 来控制你的类路径/编译/运行时需求。



编辑:应该是这样的:

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

您编译的类也应该类似目录结构:

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

然后您的baseDir应该添加到CLASSPATH中。


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.

这篇关于无法在Groovy中成功导入类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆