我目前正在使用Apache POI读取Excel文档,并且在通过IntelliJ执行主要方法时,在XSSFWorkbook的运行时出现NoClassDefFound错误。
我在build.gradle中添加了以下内容:
'org.apache.poi:poi:4.1.0',
'org.apache.poi:poi-ooxml:4.1.0'
(我也只包含poi-ooxml,因为它依赖于poi,没有运气)。
两个jar都已成功检索,当我检查IntelliJ的类路径时,会同时显示poi-ooxml和poi jar。
使用的通用代码非常简单:
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.InputStream;
public class ExcelParser {
public void parseWorkbook(InputStream inputStream) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
}
public static void main(String[] args) throws Exception {
FileInputStream file = new FileInputStream("D:\\downloads\\Book1.xlsx");
ExcelParser p = new ExcelParser();
p.parseWorkbook(file);
}
}
这是main的类路径:
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-lambda-java-core:1.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: mysql:mysql-connector-java:5.1.35" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-java-sdk-rds:1.11.327" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-java-sdk-s3:1.11.592" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-java-sdk-kms:1.11.592" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-java-sdk-core:1.11.592" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:aws-lambda-java-events:2.2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.poi:poi-ooxml:4.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.httpcomponents:httpclient:4.5.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: software.amazon.ion:ion-java:1.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.amazonaws:jmespath-java:1.11.592" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.fasterxml.jackson.core:jackson-databind:2.6.7.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.6.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: joda-time:joda-time:2.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.poi:poi:4.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.poi:poi-ooxml-schemas:4.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.commons:commons-compress:1.18" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.github.virtuald:curvesapi:1.06" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: commons-codec:commons-codec:1.12" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.commons:commons-collections4:4.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.commons:commons-math3:3.6.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.xmlbeans:xmlbeans:3.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.apache.httpcomponents:httpcore:4.4.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.fasterxml.jackson.core:jackson-annotations:2.6.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.fasterxml.jackson.core:jackson-core:2.6.7" level="project" />
现在,当我尝试使用IntelliJ通过单元测试运行此命令时,单元测试将正确执行。测试的类路径与主类路径相同,但增加了junit。通过Gradle执行测试也成功。所以我的main的类路径有问题或者IntelliJ出现了问题,但是我很茫然。有什么想法吗?