Reputation: 101
I was trying to write a program that it can read and write a .xlsx
file, the code provided below is designed to be able to write its first excel program.
package excel_reader;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelWriter{
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
// first sheet create
HSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
// first row create - 1
HSSFRow row = sheet.createRow(0);
// first cell create - 1
HSSFCell cell = row.createCell(0); // A-1
// give data into A-1 cell
cell.setCellValue("Tester");
// Output as an excel file
workbook.write(new FileOutputStream("D:\\book1.xlsx"));
workbook.close();
}
}
Somehow, it cannot write the on the excel sheet that I provided, please do help me!
Error Code:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:102)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:124)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1373)
at excel_reader.ExcelWriter.main(ExcelWriter.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 8 more
Upvotes: 2
Views: 4535
Reputation: 265
In general the java.lang.NoClassDefFoundError: shows when you are missing the required jars Check if you have added commons-math3 jar to your build path, if not please add either downloading or using maven dependency.
If your's is a maven project then add this dependency in your pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.0</version>
</dependency>
Upvotes: 4