Stedy Stedy - 2 years ago 262
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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download