英文:
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 "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)
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("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();
}
答案1
得分: 2
似乎您缺少 commons-collections4 依赖项,您可以从 这里 获取。
英文:
It seems that you are missing the commons-collections4 dependency, you can get it from here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论