Search code examples
intellij-ideajavafxbuildapache-poiartifacts

How to resolve "java.lang.NoClassDefFoundError:" while running a Jar file built using IntelliJ IDE?


I have some code which basically reads and writes data to excel file using Apache POI. I am running the code in my run and debug mode and it runs fine without any issues, but once I create a jar using the following command

Build -> Build Artifacts -> Build or Build -> Build Artifacts -> ReBuild

And then I try to run the jar I get the following error.

Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook

I tried the following:

  1. Rebuild or clean build
  2. Went to Project Settings -> Modules -> Dependencies and selected all Export check boxes

Line where the code errors out:

XSSFWorkbook workBookObj = new XSSFWorkbook(fileIn);


Solution

  • To resolve the issue recreate the "Build Artifact" following are the steps:

    1. File-> Project Settings -> Artifacts -> select the existing artifact -> - (hit remove)

    2. Delete "META-INF" folder and "MANIFEST.MF" file from src

    3. File-> Project Settings -> Artifacts -> select the existing artifact -> + (hit remove)

    4. Artifacts -> Add -> Jar -> From modules with dependencies as shown below

    enter image description here

    1. Select the class which has the main method and hit OK.

    2. Go back to Build -> Build Artificats -> Select your new artifact and click on create artifact as shown below

    enter image description here