File reading using OpenCSV

Klausos Klausos picture Klausos Klausos · Dec 23, 2014 · Viewed 7.7k times · Source

I use OpenCSV for CSV files reading and writing. It worked fine until I hsd to install earlier version of Java - i.e. Java 6. After installing it, my code stopped working:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/opencsv/CSVReader : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source)

enter image description here

enter image description here

Answer

janos picture janos · Dec 23, 2014

Check the version of the class file in your OpenCSV .jar dependency, for example:

javap -verbose -cp opencsv.jar com.opencsv.CSVReader | grep major

If this shows a version newer than 50 (= newer than Java 6) then you cannot use this jar. You need to get a .jar file built with Java 6 or older. (If it can be built with Java 6 at all.)

I just confirmed that the latest version of the source code doesn't compile with Java 6, as it uses Java 7 features (naturally). But not too long ago, as of August 2014 the project was still on Java 5. So if you download an older version of the jar it should work.

In other words, since you downgraded your Java version, you also need to downgrade your OpenCSV dependency.

UPDATE

From SourceForge, version 2.3 contains a jar file that should be suitable for you:

$ javap -verbose -cp deploy/opencsv-2.3.jar au.com.bytecode.opencsv.CSVReader | grep major
  major version: 50

Note that the package name is a bit different from the current version.