2

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
FlashSonic526
  • 101
  • 6
  • 12
  • 4
    You are missing common-math jar in your classpath. – SMA Dec 26 '19 at 06:52
  • @SMA I see, but I wonder do I have to add the dependency to the .xml file? – FlashSonic526 Dec 26 '19 at 06:59
  • 1
    Did you use a dependency management system (e.g. maven) to set up your project? If so, it should be added automatically, but there may be something wrong with your configuration (pom.xml in case of maven). If not, you'll have to add dependencies manually. – Hulk Dec 26 '19 at 07:00
  • @SMA Also, I wonder, how do I add XSSF to my code so that my program will run .xlsx file? Thank you. – FlashSonic526 Dec 26 '19 at 07:02
  • @Hulk It works somehow without the dependency XD, but you see my code above it using HSSF, which means that the output source will be .xls, but I want the program to be able to run it in .xlsx, therefore, I tried to use the XSSF, but somehow I cannot import the library and use XSSF, can you help me with that? Please? Thank you very much. – FlashSonic526 Dec 26 '19 at 07:35

1 Answers1

4

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>
Seshidhar G
  • 265
  • 1
  • 9
  • It works somehow without the dependency XD, but you see my code above it using HSSF, which means that the output source will be .xls, but I want the program to be able to run it in .xlsx, therefore, I tried to use the XSSF, but somehow I cannot import the library and use XSSF, can you help me with that? Please? Thank you very much. – FlashSonic526 Dec 26 '19 at 07:49
  • You can directly download from [here](https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.8) and add to the project buildpath If your project is maven project then add this dependency in your pom file and run mvn install ` org.apache.poi poi-ooxml 3.8 ` – Seshidhar G Dec 26 '19 at 09:00
  • I see, but it seems like now I got another problem whereas it said `Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException` can you help me? Thanks. – FlashSonic526 Dec 26 '19 at 14:52
  • Also, do I have to indicate the dependency to my project? – FlashSonic526 Dec 26 '19 at 14:53
  • could you paste the complete stack trace – Seshidhar G Dec 30 '19 at 09:38