Groovy 中的 jar 文件导入错误
类似于这个问题我有一个小的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
Similar to this question I have a small groovy test script that basically uses the example from opencsv java library:
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...");
}
I keep getting the error "unable to resolve class au.com.bytecode.opencsv.CSVReader". I even put the opencsv jar file in the same directory as my script but it does not recognize it. I assumed this was a classpath issue so I tried with -cp tag only to receive the same error.
I am running from the command line as $ groovy testg.groovy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Groovy 不会自动从当前工作目录中获取 jar。您可以使用类路径 (
-cp
) 调用 groovy,或者放入 jar 文件${user.home}/.groovy/lib
。当我像这样启动它时,你的例子对我有用:
Groovy won't automatically pick up jars from the current working directory. You can either call groovy with a classpath (
-cp <classpath>
), or put the jar file${user.home}/.groovy/lib
.Your example worked for me when I launched it like this:
导入对我来说工作正常
The import is working fine for me