简体   繁体   中英

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

I think this is a classpath issue for your application. Check the Eclipse classpath (in your Project properties) and compare it with the classpath you have set for when you run your application outside of Eclipse. The most likely culprit is the third party library that contains the com.lesswire.wita package or one of it's dependencies.

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 .

另一种可能性是应用程序正在使用依赖于较新JRE版本的库,但是您安装的JRE(在OS路径环境中的java.exe)是较旧的版本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM