在Groovy中的jar文件中导入错误

时间:2011-03-15 20:01:16

标签: java csv groovy jar

this question类似,我有一个小的groovy测试脚本,基本上使用opencsv java库中的示例:

import au.com.bytecode.opencsv.CSVReader

CSVReader reader = new CSVReader(new FileReader("GroovyTest.csv"));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }

我一直收到错误“无法解析类au.com.bytecode.opencsv.CSVReader”。我甚至将opencsv jar文件放在与我的脚本相同的目录中,但它无法识别它。我认为这是一个类路径问题,所以我尝试使用-cp标签只接收相同的错误。

我从命令行运行$ groovy testg.groovy

2 个答案:

答案 0 :(得分:1)

Groovy不会自动从当前工作目录中获取jar。您可以使用类路径(-cp <classpath>)调用groovy,也可以将jar文件${user.home}/.groovy/lib调用。

当我这样推出时,你的例子对我有用:

groovy -cp opencsv-2.2/deploy/opencsv-2.2.jar testg.groovy

答案 1 :(得分:-1)

import au.com.bytecode.opencsv.*

导入对我来说很好