Wednesday, August 20, 2014

Trouble Using the Jar Design Element in XPages

It has been a while since posting, I have not forgot about blogging, but really have not done anything blog worthy.  In one of my projects, we have to talk to a mainframe application (not the tables) and perform various tasks.  The mainframe connection is made through Hostbridge technology, and packaged for me by our java developer in a jar that I can interact with using the java beans I use for business logic in my application.

As we are in proof of concept mode, the java developer sends me the jar every time he makes a change and I place it in my NSF using the new Jar design element. Unfortunately, so far it hasn't been as easy doing this as I expected.  I dropped the jar file in, and then was able to write code to call the public methods using the java bean.  So far so good, after adding the proper imports the code compiled fine.

At runtime, it would choke and give me a NoClassDefFoundError exception. This of course is really confusing because to me, the time to decide that you can't find the class should be compile time. I tried several times cleaning and rebuilding the application before turning to Stack Overflow with this question: http://stackoverflow.com/questions/25371817/the-contents-of-jar-file-not-found-using-jar-design-element-of-designer-9

I recieved two helpful answers from Stephan Wissell and Frantisek Kossuth.  The first thing I tried was removing the application, re-adding and verifying the build path which Stephan suggested.  This worked, and I was able to continue on without getting the same exception.

The next day it was necessary to change the package name we were using inside the jar.  I was able to fix my own java code by correcting the import and again it compiled fine.  Unfortuately at runtime,  I started getting the same NoClassDefFoundError exception again.  This time nothing I tried worked.  I spent a long time checking build paths, and trying to use the WebContent/WEB-INF/lib instead of the jar element.  I did discover along the way that the jar element actually places the file in the the lib folder on its own.  I also discovered that the Package Explorer makes it appear that the file is not there, but if you use the Navigator instead you can see that the jar file is present in that folder.

Franitisek in his answer, linked to another Stack Overflow question, one that I discovered before I asked my question.  In that question, the person who answered Panu Haaramo made a comment on his answer directing the OP to this blog post: http://lotusandjava.blogspot.com/2012/10/xpage-javalangnoclassdeffounderror.html

In this post, the author remarked that the only thing that worked was renaming the class.  So instead of renaming any classes, I decided to rename the jar file using the right-click menu option rename.  I did not rename using the O/S or have the java developer send me a new jar.  To my amazement, this worked, and the exception went away and I am now back in business.  I probably spent at least 4 hours at work today struggling with this issue, so I am hoping this post will save someone that trouble.

The bottom line

If you are getting a NoClassDefFoundError exception and using jars, try renaming the jars using the right click menu. It is easy, and it just might work. (You can always rename them back later, although I haven't actually tried that yet)

If someone at IBM sees this post, this might be something to look into fixing in the next release. :)


PS.  I am looking forward to attending MWLUG next week, hope to see some of you there.

4 comments:

  1. Yes, the NoClassDefFound error is really annoying. Sorry, I don't have a proper solution for that. Just wanted to share couple of tips about working with JAR files: they can be easily added from file system with drag-n-drop (in Package Explorer or Navigator). Then you can add them to build path with right-click (Build Path --> Add to Build Path). Also, you mentioned not seeing the JAR files in Package Explorer. Actually, they should be there, under the section "Referenced Libraries". If they are not there, your build path is broken.

    ReplyDelete
    Replies
    1. Thanks Lauri for your comment. Yes, it did show under referenced libraries, but I was confused I couldn't find it under lib. I was trying everything possible to fix this. My guess is that the renaming is causing some other under the covers configuration file to be rebuilt,and that should be happening when you remove the jar and add it back but does not.

      Delete
  2. I've had this problem as well. Doing a Project, Clean ... usually fixes it for me.

    To further comment on what Lauri said, I usually:
    For a new jar file:
    A1. drag and drop the jar file in to a "lib" folder I've created under WebContent\WEB-INF in the package explorer.
    A2. Right click on the database: Build Path, Configure Build Path, Libraries tab, Add Jars... button. This adds the jar to the list in the Libaries tab and hides the jar file from the "lib" folder.
    A3. I can now import the class in to my Java classes.

    If I need to replace the jar file:
    B1. Go to the Libraries tab (where it was added in A2), select the jar and click on the Remove button. This removes it from the Libraries list and un-hides the jar file in the "lib" folder.
    B2. Delete the jar file from the "lib" folder (right click and Delete) and repeat A1 and A2 above with the new jar file.
    B3. Click on the database in package explorer and Project, Clean ...

    ReplyDelete
  3. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.

    ReplyDelete