opencsv在从主函数调用时抛出异常,并且存在module-info.java。

huangapple go评论84阅读模式
英文:

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

问题

我想在使用Java 14构建的项目中(该项目使用Java模块系统),使用opencsv。因此,项目中有一个module-info.java文件。

从单元测试中发起对opencsv中方法的调用是正常的,但从Main.main()方法发起调用时会抛出异常。如果我移除module-info.java文件,该方法在两种情况下都能正常工作。

module-info文件很简单,但可能是错误的:

module opencsvDemo {
    requires opencsv;
    opens demo;
}

从Main.main()运行时,我得到以下错误:

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

这个错误是我的module-info.java文件中出错了,还是应该在其他地方寻找问题?

英文:

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 &quot;main&quot; java.lang.NoClassDefFoundError: java/sql/Date
	at commons.beanutils@1.9.4/org.apache.commons.beanutils.ConvertUtilsBean.registerOther(ConvertUtilsBean.java:730)
	at commons.beanutils@1.9.4/org.apache.commons.beanutils.ConvertUtilsBean.deregister(ConvertUtilsBean.java:602)
	at commons.beanutils@1.9.4/org.apache.commons.beanutils.ConvertUtilsBean.&lt;init&gt;(ConvertUtilsBean.java:161)
	at opencsv@5.2/com.opencsv.bean.ConverterPrimitiveTypes.&lt;init&gt;(ConverterPrimitiveTypes.java:88)
	at opencsv@5.2/com.opencsv.bean.AbstractMappingStrategy.determineConverter(AbstractMappingStrategy.java:739)
	at opencsv@5.2/com.opencsv.bean.HeaderColumnNameMappingStrategy.loadAnnotatedFieldMap(HeaderColumnNameMappingStrategy.java:155)
	at opencsv@5.2/com.opencsv.bean.AbstractMappingStrategy.loadFieldMap(AbstractMappingStrategy.java:405)
	at opencsv@5.2/com.opencsv.bean.AbstractMappingStrategy.setType(AbstractMappingStrategy.java:343)
	at opencsv@5.2/com.opencsv.bean.util.OpencsvUtils.determineMappingStrategy(OpencsvUtils.java:77)
	at opencsv@5.2/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

得分: 2

你可以尝试在你的模块描述符中使用

requires java.sql;

以确保能够正确解析带有所需类的模块。

如果代码在没有这个描述符的情况下编译通过,那么肯定是另一个模块负责了这一点。你需要识别出它,并决定选择的进一步路径。这可能是因为你的项目依赖引入了带有相同包的自动模块,这可能会进一步导致分割包问题。在这种情况下,寻找支持Java升级的库更新等可能会有所帮助。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月10日 20:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64293538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定