java – NoClassDefFoundError org / apache / poi / ss / usermodel / Workbook
I'm running a shell script that calls Java classes to get some data from the database and use the data to create an excel report I get the error exception Java in the thread "main" Lang.noclassdeffounderror: org / Apache / POI / SS / usermodel / workbook when the code hits the next line in my java class:
XSSFWorkbook workbook = new XSSFWorkbook ();
This is how I define 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 confirmed that I had downloaded the jar (via gradle), so I tried to understand what I was missing here Can someone help me with this?
Stack trace:
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
Solution
According to this Apache POI FAQ entry, mixing POI cans between different versions is not supported, and will be interrupted in various ways, such as those you find Don't do that!
You need to use all poi tanks in the same version I suggest the latest version, available here (currently 3.15)
You should probably also check the components and their dependencies page to make sure you have all the jar files you need to use Apache poi Well, or use dependency management tools like Maven or gradle to handle it for you!