1

I am currently using vscode and apache poi, created a program to automatically create a .xlsx program and have A1 cell input a String called "Tester", and that error pops up.

Codes in my program:

package excel_reader;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelWriter {
    public static void main (String[] args) throws Exception {
        //ExcelReader eR = new ExcelReader();
        XSSFWorkbook workbook = new XSSFWorkbook();  // here is the7 line 13
        // first sheet create
        XSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
        // first row create - A
        XSSFRow row = sheet.createRow(0);
        // first cell create - 1
        XSSFCell cell = row.createCell(0); // A-1
        // give data into A-1 cell
        cell.setCellValue("Tester");

        // Output as an excel file
        workbook.write(new FileOutputStream("C:\\Users\\Sonic\\Desktop\\book.xlsx"));
        workbook.close();

        // C:\\Users\\Sonic\\Desktop\\book.xlsx
    }
}

Error Code:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell aka.ms/pscore6

PS E:\excel_reader_v.1_beta1-0> cd 'e:\excel_reader_v.1_beta1-0'; & 'C:\Users\Sonic\.vscode\extensions\vscjava.vscode-java-debug-0.24.0\scripts\launcher.bat' 'C:\Users\Sonic\AppData\Local\Programs\AdoptOpenJDK\bin\java' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Sonic\AppData\Local\Temp\cp_5wuvlu562pjj6rfd2pconxvl4.jar' 'excel_reader.ExcelWriter' 
Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:161)
        at excel_reader.ExcelWriter.main(ExcelWriter.java:13)

pom.xml (dependency):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>3.1.0</version>
</dependency>

Apache POI is new to me, please do help me, I will be very appreciated, thank you very much.

13
  • Can you please mark the line numbered 18 in your code (file ExcelWriter.java). This seems to be the line that causes the exception.
    – hc_dev
    Dec 26, 2019 at 19:19
  • 2
    Any reason for extending ExcelReader? Dec 26, 2019 at 19:19
  • 1
    I just tried running your code. the only part I changed was removed Excel Reader entirely and then gave path to the file explicitly. It is working fine for me.
    – Brooklyn99
    Dec 26, 2019 at 19:35
  • @hc_dev mark the line numbered 18? May I ask what do you mean by marking line number 18? thank you, just confusing. Dec 26, 2019 at 20:00
  • @MehulGayate you right, for no reason, I should be double checking it first, but I wonder is it because of extending ExcelReader thus causing the exception? Thank you, I wish that I can understand and remember the mistake that I made to prevent having the same mistake next time, thank you. Dec 26, 2019 at 20:02

3 Answers 3

12

Cause of java.lang.NoSuchFieldError

If you search StackOverflow for tag you will find a lot of similar questions. Most of them have 2 things in common:

This version mismatch may also cause your error:

java.lang.NoSuchFieldError: DEFAULT

Since the constant DEFAULT of class org.apache.poi.ss.formula.udf.UDFFinder existed in versions prior to 3.17, but now (versions 4.0 and newer) has been deprecated. Means, it does not exist in version 4.0 and above.

Problem at runtime: If statement new XSSFWorkbook(); (at line 13) is executed using dependency (JAR) poi-ooxml, then it uses a UDFFinder of the core dependency (JAR) poi and thus tries to find the CONSTANT field DEFAULT (which may not be there, since versions mismatch).

So I suppose, your versions of Apache poi and poi-ooxml are mismatching (not the same level). See similar question with version mismatch using Apache Poi.

Solution: matching versions!

If you are using Maven, make sure you have these dependencies on your POM:

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>

At least both have to be the same version!

If you are managing your classpath and JARs manually, watch out for the filename of all apache-poi JAR files and make sure the versions are same.

Code seems very close to working

If you just want to write your freshly created Excel-Workbook to a file, e.g. /tmp/MyFirstExcel.xlsx (on Linux) or C:/Users/Sonic/MyFirstExcel.xlsx (on Windows, supposed your Windows-User is Sonic) then use this code:

public class ExcelWriter {
    static final String FILE_PATH_LINUX = "/tmp/MyFirstExcel.xlsx";
    static final String FILE_PATH_WIN = "C:/Users/Sonic/MyFirstExcel.xlsx"; // note: replace Window's backslash '\' by either double-backslash '\\' or single forward-slash '/'

    public static void main(String[] args) throws FileNotFoundException, IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("FirstExcelSheet"); // first sheet create
        XSSFRow row = sheet.createRow(0); // first row create - A
        XSSFCell cell = row.createCell(0); // first cell create - A-1        
        cell.setCellValue("Tester"); // give data into A-1 cell

        // Output as an excel file
        workbook.write(new FileOutputStream(FILE_PATH_WIN));
        workbook.close();
    }
}

Unclear usage of class ExcelReader: There is no obvious reason to extend from ExcelReader. Thus and for simplicity you can omit the extends ExcelReader, declaration of ExcelReader eR = new ExcelReader(); and usage eR.getImpoFileLink() to get the file-path. Otherwise please post the source-code of class ExcelReader to your question, to make it a minimal reproducible example.

Minimal working solution (above) explained: Instead just put a file-path to desired folder and file, like constant FILE_PATH_WIN here. I saw your related question few hours ago, where you did that.

Supposed you fixed your other issues/questions here, as well as resolved the version-mismatch with POI dependencies here, the solution given above should compile and write out the desired Excel workbook (.xlsx) with sheet FirstExcelSheet and cell A1 containing string Tester.

See also

See this tutorial on Apache POI – Reading and Writing Excel file in Java.

6
  • I see, I thought that if I need to use the instance data (file path) from another class (ExcelReader, in the same package), I will have to make extend the class and make it to be a subclass thus I can use the instance data from it. Dec 26, 2019 at 20:06
  • I also wonder that if I want the program to group the same group of Strings (compare strings), but the only thing I can give is the column in excel file, is it possible to do that? I know that this seems confusing, I will explain it more detail if needed, but please do help me with it because I am stuck with it for a long time, thank you very much. Dec 26, 2019 at 20:09
  • @FlashSonic526 (1) Understood but need the full code of class ExcelReader see comment to your question. (2) Don't understand: group the same group of Strings (compare strings). Maybe add an example to your question, better: ask in a new question and link both questions :-)
    – hc_dev
    Dec 26, 2019 at 21:30
  • but the problem still does occur after removing extend ExcelReader, I put the file path directly to workbook.write(new FileOutputStream(C:\\Users\\<username>\\Desktop\\book1.xls, it doesn't work, I put the link and store it into a constant String and use it, it still doesn't work, please do help me. Dec 27, 2019 at 7:46
  • I updated the entire stack trace, did not see anything that does has Caused by: but I updated the whoe stack trace, please take a look, thank you. Dec 27, 2019 at 7:53
0

Because putting this in the comment would not be feasible. Could you try to remove this portion of code in the pom.xml and try it again?

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>

based on cursory reading. it has been defined in the bellow code

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>
6
  • I supposed that you meant removing poi-ooxml 3.11 dependency, and keep poi-ooxml 4.1.1 dependency? Dec 30, 2019 at 9:10
  • yes. that's right :) seems like the field itself is not exists in 3.11 version Dec 30, 2019 at 10:24
  • Sorry for the late response, I need some clarifying, what you meant "the field itself is not exists in 3.11 version", do you mean that by removing the <dependency> only or remove the .jar file and dependency? Thanks. Jan 2, 2020 at 0:12
  • 1
    what I mean is you need to remove the <dependency> tag. and afterward, you need to "package" your application again to ensure the aforementioned dependency has been removed from your jar file (so, by that extension, it would also remove the .jar file). Jan 2, 2020 at 5:25
  • Thanks for your information, but just to let you know that since I do not know much about java and I only have 4 months experience with it, therefore, the dependency file was created by creating a new file in the lib folder and change the file type to .pom, so does these steps automatically add a "<dependency> tab" to the package? Jan 9, 2020 at 14:15
0

The only apache-poi dependencies you need in pom.xml are

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0</version>
</dependency>

Please remove others.

When I removed the others, my code worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.