opencsv throws exception when called from main, and module-info.java is present

495 views Asked by At

I want to use opencsv in a project that is build with Java 14, using Java module system. So it has a module-info.java file.

A call to a method in opencsv works fine when it's initiated from a unit test, but throws an exception when initiated from the Main.main() method. If I remove the module-info.java file, the method works fine in both cases.

The module-info file is simple, but probably wrong:

module opencsvDemo {
    requires opencsv;
    opens demo;
}

When running from Main.main(), I get

Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/Date
    at [email protected]/org.apache.commons.beanutils.ConvertUtilsBean.registerOther(ConvertUtilsBean.java:730)
    at [email protected]/org.apache.commons.beanutils.ConvertUtilsBean.deregister(ConvertUtilsBean.java:602)
    at [email protected]/org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:161)
    at [email protected]/com.opencsv.bean.ConverterPrimitiveTypes.<init>(ConverterPrimitiveTypes.java:88)
    at [email protected]/com.opencsv.bean.AbstractMappingStrategy.determineConverter(AbstractMappingStrategy.java:739)
    at [email protected]/com.opencsv.bean.HeaderColumnNameMappingStrategy.loadAnnotatedFieldMap(HeaderColumnNameMappingStrategy.java:155)
    at [email protected]/com.opencsv.bean.AbstractMappingStrategy.loadFieldMap(AbstractMappingStrategy.java:405)
    at [email protected]/com.opencsv.bean.AbstractMappingStrategy.setType(AbstractMappingStrategy.java:343)
    at [email protected]/com.opencsv.bean.util.OpencsvUtils.determineMappingStrategy(OpencsvUtils.java:77)
    at [email protected]/com.opencsv.bean.CsvToBeanBuilder.build(CsvToBeanBuilder.java:210)
    at opencsvDemo/demo.CsvGetter.get(CsvGetter.java:23)
    at opencsvDemo/demo.Main.main(Main.java:13)

Is the error in my module-info.java file, or should I look somewhere else?

1

There are 1 answers

1
Naman On BEST ANSWER

You can try using

requires java.sql;

in your module descriptor to ensure the module with the expected class is resolved correctly.

If the code compiled without that descriptor, there must be another module responsible for that. You would have to identify it and decide, further on the path to be chosen. It could have been due to dependencies of your project bringing in automatic modules with the same package, which could further cause a split package issue as well. In which case, looking for library updates, etc in favor of Java upgrade might help.