I am running a shell script which calls a java class to get some data from database and create an excel report with that data. I get the error Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook when the code hits the below line in my java class:
XSSFWorkbook workbook = new XSSFWorkbook ();
This is how I have defined the classpath:
CLASSPATH=${CLASSPATH}:<path-to-jars>/poi-2.5.1-final-20040804.jar
CLASSPATH=${CLASSPATH}:<path-to-jars>/poi-ooxml-3.11.jar
I verified that the jars have been downloaded(via gradle), so trying to understand what am I missing here. Can someone please help me with this?
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.test.ExcelReportGenerator.writeExcel(ExcelReportGenerator.java:26)
at com.test.ReportRunner.createReport(ReportRunner.java:109)
at com.test.ReportRunner.main(ReportRunner.java:93)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 15 more
As per this Apache POI FAQ entry, mixing POI jars between different versions is not supported and will break in all sorts of ways, such as the one you've found. Don't do it!
You need to be using your POI jars all from the same release. I'd suggest the latest version, available here (currently 3.15)
You should probably also review the components and their dependencies page, to ensure you've got all the required jars for your use of Apache POI. Well, or use a dependency management tool like Maven or Gradle to handle that for you!