sonik
sonik

Reputation: 101

java.lang.NoSuchFieldError: DEFAULT

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.

Upvotes: 1

Views: 10850

Answers (3)

likejudo
likejudo

Reputation: 3726

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.

Upvotes: 0

kucing_terbang
kucing_terbang

Reputation: 5131

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>

Upvotes: 0

hc_dev
hc_dev

Reputation: 9377

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.

Upvotes: 12

Related Questions