Stedy Stedy - 4 months ago 6
Java Question

Import error in jar file in Groovy

Similar to this question I have a small groovy test script that basically uses the example from opencsv java library:


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". 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


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:

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