0

I'm learning selenium and trying to run the below code but it throws the exception .NoSuchFieldError: EMPTY_BYTE_ARRAY . Please help me understand what is going wrong with this simple program.

package newpackage;

import java.io.FileInputStream;
import java.io.IOException

import org.apache.poi.xssf.usermodel.*;


public class spp {      

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    String excelFilePath = "./src/DataFiles/GMD.xlsx";
    FileInputStream inputStream = new FileInputStream(excelFilePath);
    
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    XSSFSheet sheet = workbook.getSheetAt(0);
    int rows = sheet.getLastRowNum();
    System.out.println(rows);   
    
}
}

Console:

Exception in thread "main" java.lang.NoSuchFieldError: EMPTY_BYTE_ARRAY

at org.apache.logging.log4j.core.config.ConfigurationSource.<clinit>(ConfigurationSource.java:56)
at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:32)
at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:85)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.createContext(ClassLoaderContextSelector.java:254)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:136)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:123)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:117)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:150)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:599)
at org.apache.poi.ooxml.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:56)
at newpackage.spp2.main(spp2.java:17)
Priyanka
  • 21
  • 1
  • 2
  • you are trying to access a field that doesn't exist. what line do you get that exception at? also: for future reference: javascript and java are not the same – Stultuske Jan 04 '22 at 12:21
  • This generally happens when you have incompatible versions of dependencies. – Mark Rotteveel Jan 04 '22 at 12:28
  • XSSFWorkbook workbook = new XSSFWorkbook(inputStream); – Priyanka Jan 04 '22 at 12:33
  • 1
    No that is not where the exception is thrown. Please show us the **complete** stacktrace. – Stephen C Jan 04 '22 at 12:47
  • Updated the question. Please check – Priyanka Jan 04 '22 at 12:56
  • Seems consistent with a log4j version mismatch. Are you using maven? You might want to check your pom.xml. https://stackoverflow.com/questions/70333905/spring-boot-log4j2-version-2-15-0-empty-byte-array-error-in-wildfly-server – matt Jan 04 '22 at 13:16

1 Answers1

5

OK so this is nothing to do with the excel file that you are loading or (probably) your application code at all.

What is happening is that when

org.apache.logging.log4j.core.config.ConfigurationSource 

is being initialized ... deep in the log4j 2.x implementation code ... it is running into binary compatibility problem. The code for ConfigurationSource that is being depends on a class called

org.apache.logging.log4j.util.Constants

and is trying to use a constant in that class called EMPTY_BYTE_ARRAY. That field was present when the ConfigurationSource class was compiled. But it is NOT present in the version of Constants that the classloader found ... which happens to match the most recent version of the log4j 2.x source code.

Now org.apache.logging.log4j.core.config.ConfigurationSource is in the log4j-core dependency while org.apache.logging.log4j.util.Constants is in log4j-api.

So this strongly suggests that you have non-matching versions of the log4j-core and log4j-api classes on the (runtime) classpath for your application.


(I previously thought this was due to bad patching, but I missed the fact that there are two distinct Constants classes in the log4j code base.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • [This question](https://stackoverflow.com/questions/70474648/wildfly-16-log4j-2-17-0-nosuchfielderror-empty-byte-array) suggests it is due to incompatible versions of log4j-api and log4j-core on the classpath. – Mark Rotteveel Jan 04 '22 at 13:31
  • You are correct. There are two `Constants` classes `org.apache.logging.log4j.core.util.Constants` and `org.apache.logging.log4j.util.Constants`, and `ConfigurationSource` depends on the latter. – Stephen C Jan 05 '22 at 01:34
  • 1
    Thanks everyone for pitching in. Problem is due to incompatible versions of log4j-api and log4j-core .jar files. Added correct versions, now it is working. – Priyanka Jan 05 '22 at 05:16
  • What are the compatible versions? Im stuck with the same issue – Meet Shah Mar 04 '23 at 00:01