在编写Excel文件时遇到java.lang.NoClassDefFoundError错误。

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

Getting java.lang.NoClassDefFoundError error when writing Excel file

问题

我将开发一个工作流,其中我需要从Oracle数据库中获取一些数据,并将结果集写入Excel文件中。在开始之前,我想进行一些测试。然而,我遇到了以下运行时异常 java.lang.NoClassDefFoundError。详细信息如下。我知道我可能遗漏了一些非常愚蠢的东西,但我正在努力找出问题所在。感谢您的帮助。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at DatabaseConnPkg.DatabaseConnClass.main(DatabaseConnClass.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
C:\Users\Erfan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

这些是我正在使用的外部 JAR 包:

  • poi-4.1.2.jar
  • poi-ooxml-4.1.2.jar

以下是我正在使用的代码:

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ResultSetMetaData resultSetMetaData = null;

try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","hr","hr");
    String sql = "select * from countries";
    preparedStatement = connection.prepareStatement(sql);
    resultSet = preparedStatement.executeQuery();
    resultSetMetaData = resultSet.getMetaData();
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet spreadSheet = workbook.createSheet();
    XSSFRow row = spreadSheet.createRow(1);
    XSSFCell cell;

    for(int i=0; i<resultSetMetaData.getColumnCount(); i++) {
        cell = row.createCell(i);
        cell.setCellValue(resultSetMetaData.getColumnName(i));
    }

    while(resultSet.next()) {
        row = spreadSheet.createRow(1);
        cell = row.createCell(1);
        cell.setCellValue(resultSet.getString(1));
        cell = row.createCell(2);
        cell.setCellValue(resultSet.getString(2));
        cell = row.createCell(3);
        cell.setCellValue(resultSet.getString(3));
    }

    FileOutputStream fileOutputStream = new FileOutputStream(new File(FILE));
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    System.out.println("exceldatabase.xlsx written successfully");

    //System.out.println(resultSetMetaData.getColumnName(1) + "\t" + resultSetMetaData.getColumnName(2) + "\t" + resultSetMetaData.getColumnName(3));

    /*
    while(resultSet.next()) {
        System.out.println(resultSet.getString(1) + "\t\t" + resultSet.getString(2) + "\t\t" + resultSet.getString(3));
    }
    */

    connection.close();
} catch (Exception e) {
    e.printStackTrace();
}
英文:

I will be developing a workflow where I would be needed to fetch some data from Oracle DB and write the ResultSet in Excel file. Before starting that, I would like to do some tests. However, I am getting the following runtime exception java.lang.NoClassDefFoundError. Details can be found below. I know I am missing something very silly but I am struggling to find that out. I appreciate your help.

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at DatabaseConnPkg.DatabaseConnClass.main(DatabaseConnClass.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
C:\Users\Erfan\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

These are the external jars that I am using

poi-4.1.2.jar

poi-ooxml-4.1.2.jar

This is the code that I am working with

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ResultSetMetaData resultSetMetaData = null;
try {
Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);
connection = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:orcl&quot;,&quot;hr&quot;,&quot;hr&quot;);
String sql = &quot;select * from countries&quot;;
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
resultSetMetaData = resultSet.getMetaData();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadSheet = workbook.createSheet();
XSSFRow row = spreadSheet.createRow(1);
XSSFCell cell;
for(int i=0; i&lt;resultSetMetaData.getColumnCount(); i++) {
cell = row.createCell(i);
cell.setCellValue(resultSetMetaData.getColumnName(i));
}
while(resultSet.next()) {
row = spreadSheet.createRow(1);
cell = row.createCell(1);
cell.setCellValue(resultSet.getString(1));
cell = row.createCell(2);
cell.setCellValue(resultSet.getString(2));
cell = row.createCell(3);
cell.setCellValue(resultSet.getString(3));
}
FileOutputStream fileOutputStream = new FileOutputStream(new File(FILE));
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println(&quot;exceldatabase.xlsx written successfully&quot;);
//System.out.println(resultSetMetaData.getColumnName(1) + &quot;\t&quot; + resultSetMetaData.getColumnName(2) + &quot;\t&quot; + resultSetMetaData.getColumnName(3));
/*
while(resultSet.next()) {
System.out.println(resultSet.getString(1) + &quot;\t\t&quot; + resultSet.getString(2) + &quot;\t\t&quot; + resultSet.getString(3));
}
*/
connection.close();
} catch (Exception e) {
e.printStackTrace();
}

答案1

得分: 2

似乎您缺少 commons-collections4 依赖项,您可以从 这里 获取。

英文:

It seems that you are missing the commons-collections4 dependency, you can get it from here

huangapple
  • 本文由 发表于 2020年5月30日 22:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/62103843.html
匿名

发表评论

匿名网友

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

确定