Search code examples
javaeclipsedocx4jinvocationtargetexception

How to prevent InvocationTargetException when jar is running?


I run into a weird (for me at least) case. When i'm running my project from Eclipse everything is fine. But when I export the project to a jar file it fails with InvocationTargetException. I'm using other external libs and everything runs fine (program runtime is about 5 min) and it creates a docx at the end.

try {
  WordprocessingMLPackage wordMLPackage;
  wordMLPackage = WordprocessingMLPackage.createPackage(); // this is Line 1320
  // more lines here
} catch (Exception e) {
  e.printStackTrace();
}

I'm running in cmd and it prints:

Exception in thread "main" java.lang.reflect.InvocationTargetException

Caused by: java.lang.ExceptionInInitializerError

at com.lesswire.wita.Wita.CreateReport(Wita.java:1320)

How could i make it work in my jar ?

Thanks, roncsak


Solution

  • After all, the main reason of this bug was related Log4jConfigurator as @alexei-kaigorodov guessed. But! log4j was configured but not well for docx4j! I've searched logj.xml files on the net, and tried some.

    Here is how its done:

    In eclipse, make sure log4j.xml is in a class folder (often referenced this as CLASS PATH). If you don't want to put there, you can create and set one. Open Java Build Path in Eclipse and navigate to the Libraries tab. Then choose "Add Class Folder" and select the newly created folder.

    For some reason when I do a Clean Build the log4j.xml is overwritten. Make sure your logj4.xml is right after Clean.