0

I got a very strange problem that my program runs in Eclipse but doesn't run well wen exported as a runnable jar. I am getting ClassNotFoundException.

C:\Users\43156557\workspace\CAF3_IDE\MFTRelease>java -jar MSDocWriter.jar
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
        at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:149)
        at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:136)
        at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:81)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:220)
        at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
        at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:1
11)
        at com.hsbc.glt.automation.test.mft.util.MSDocumentWriter.initializeDocu
ment(MSDocumentWriter.java:92)
        at com.hsbc.glt.automation.test.mft.util.MSDocumentWriter.main(MSDocumen
tWriter.java:70)
        ... 5 more
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 14 more

C:\Users\43156557\workspace\CAF3_IDE\MFTRelease>
RHA
  • 3,677
  • 4
  • 25
  • 48
Terry Ho
  • 1
  • 1
  • Looks like your jar file doesn't include dependencies. Check http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – user2953113 Sep 18 '15 at 19:05
  • Look at your manifest file in the jar and on eclipse i bet the would be different. That is why you see this error. – StackFlowed Sep 18 '15 at 19:08
  • @user2953113 I believe if the jar would be missing the he would no class found error not class def not found. – StackFlowed Sep 18 '15 at 19:10
  • possible duplicate of [What is the difference between NoClassDefFoundError and ClassNotFoundException?](http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception) – StackFlowed Sep 18 '15 at 19:13

1 Answers1

0

ClassNotFoundException usually depicts that JVM is not able to load the required class file from the defined classpath (either the path is not correct or the class / jar is not present at the path).

The Error in current scenario might be cause have missed to include the Dependency libraries when creating the jar file. Since on eclipse you have the class-path correctly including those its working perfectly.

Make sure that you check "Package Required Jar into generated JAR" , this will include the jar files automatically from your eclipse build path.

for detailed info this might come handy Include Dependent Jars in Executable Jar

Community
  • 1
  • 1
Akash Yadav
  • 2,411
  • 20
  • 32
  • I believe if the jar would be missing the he would no class found error not class def not found. – StackFlowed Sep 18 '15 at 19:09
  • Hmm, but i saw java.lang.ClassNotFoundException: org.dom4j.DocumentException , isnt that a result of class not being available at path ? – Akash Yadav Sep 18 '15 at 19:11
  • Yes that is because the manifest doesn't have it ... not because the jar is missing. check this for details http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception – StackFlowed Sep 18 '15 at 19:12
  • Makes sense, But since he is using Eclipse export i assumed Manifest is autogenerated :P maybe am wrong – Akash Yadav Sep 18 '15 at 19:15
  • 1
    No you are exactly correct that manifest is auto generated but sometimes it doesn't do a good job at that so you delete it and rebuild your project. Then it should fix that error. – StackFlowed Sep 18 '15 at 19:16