3
votes

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

3

3 Answers

4
votes

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.

1
votes

When OpenCSV was upgraded to 3.0 the decision was made to upgrade to Java 7. This was done for several reasons. The main reason being that the largest reason was that the main fixes in 3.0 was to support the lazy checks in Lambdas that are in Grails (possibly Java 8) so it was requested the code be compiled in Java 7 because it is more optimal for Grails. I allowed that because Oracle had announced it was dropping support for Java 7 in 2015 and Java 5 & 6 had long since been unsupported plus I had a small number of emails asking when I was going to port opencsv to java 7 (and only one for java 8 for some reason) so I felt that moving to java 7 was safe. The last was that I develop using a mac and no longer have access to a java 5 compiler and I really did not feel good about pushing out a Java 5 version of opencsv built using java 7.

In hindsight seeing the issues that this caused (you are the third that I know of that cannot upgrade because of this) I would have left it at Java 5 and asked the Grails users to perform a performance test and myself build opencsv in a Ubuntu VM on my mac. But since it is out I don't want to downgrade and possibly effect users as well.

If you cannot upgrade your project to java 7 you do have two options: the first is that on the sourceforge and maven repositories you can download the older versions. So 2.4 and earlier was built using java 5. The other option is that you can download the code and build it yourself. Thanks to KommradHomer the current code base is compatible with Java 6 and will build as is with a Java 6 compiler (you just need to change the pom file).

0
votes

I am seeing the same issue. Please let me know which version of openCSV I can use to be compatible with java version "1.6.0_22" ? OpenCSV 2.3 does not work for me.