8 Replies Latest reply on Apr 8, 2002 3:10 AM by user57

    JNI Libraries, Classloaders and redeployment

    redsolo

      We're developing a Servlet that uses a JNI library and have managed to deploy this through our web-host server. The server is using JBoss and re-detects and reloads new servlet .wars when necessary. This have functioned very good until today, when we finnaly got our JNI code working through the autoconfiguring that JBoss do. (earlier post in this forum)


      Now we discovered that when our servlet is re-deployed it tries to re-load the JNI library, so we can use it in the servlet. But using the System.loadLibrary() throws a Throwable that states :

      java.lang.UnsatisfiedLinkError: Native library /dunder1/home/userC/c0017200/public_html/royal11.levonline.com/deploy/bin/libJpeg2000ServletDecoder.so already loaded in another classloader
      at java.lang.ClassLoader.loadLibrary0 ClassLoader.java:1346)
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1306)
      at java.lang.Runtime.loadLibrary0(Runtime.java:749)
      at java.lang.System.loadLibrary(System.java:820)
      at com.imbridge.iwip.servlet.IwipServlet.doGet(IwipServlet.java:136)

      Code :
      try
      {
      System.loadLibrary("Jpeg2000ServletDecoder");
      } catch ( UnsatisfiedLinkError e )
      {}


      And it seems that we must load the library again for the servlet since we trying to access the JNI method it throws this message:

      java.lang.UnsatisfiedLinkError: decodeJP2Impl
      at com.imbridge.iwip.servlet.codec.Jpeg2000ServletDecoder.decodeJP2Impl(Native Method)
      at com.imbridge.iwip.servlet.codec.Jpeg2000ServletDecoder.decodeJP2(Jpeg2000ServletDecoder.java:31)
      at com.imbridge.iwip.servlet.ImageFileArchiver.saveImage(ImageFileArchiver.java:294)
      at com.imbridge.iwip.servlet.ImageFileArchiver.archive(ImageFileArchiver.java:175)


      Do anyone have ANY clue on how we can unload the library when closing the servlet, and then re-load it the next time ???

      Any help is appreciated

      //Erik

        • 1. Re: JNI Libraries, Classloaders and redeployment

          Tomcat doesn't support this.

          In the other thread, I suggested writing an MBean to
          load/unload the library.

          Regards,
          Adrian

          • 2. Re: JNI Libraries, Classloaders and redeployment
            redsolo

            Ok. I will try to find that thread here in the forums.

            If I load the library in the MBean, can all my servlets access it then ?? Or do they have to go through the Bean ?
            BTW what is a MBean ? Where can i find more info about how to write a mbean ??

            • 3. Re: JNI Libraries, Classloaders and redeployment

              An MBean is a managed bean. They are used in the
              core JBoss architecure.

              If you look at jboss.jcml, these are all mbeans.

              You can use the mbean to load a class that wraps the
              native implementation. The mbean is loaded at server
              start, i.e. the code only happens once.
              The servlets can then reference that class, probably
              through some sort of static factory method?

              The trouble with Tomcat is that when you redeploy,
              you get a new classloader, so the class is reloaded.
              But the old class is probably still on the heap waiting
              to be garbage collected.

              Regards,
              Adrian

              • 4. Re: JNI Libraries, Classloaders and redeployment
                davidjencks

                I am not an expert on JNI, however when I investigated I got the strong impression that java goes to great lengths to prevent you from loading a native library twice in the same vm under any circumstances. I would certainly like to know if I am wrong, preferably by means of an example;-)

                • 5. Re: JNI Libraries, Classloaders and redeployment
                  jules_gosnell


                  I had a similar problem with a servlet that I was developing.

                  I split the JNI stuff off into another servlet, which needed no further development. The servlet which was being actively developed then consumed the jni based one.

                  The second servlet could then be bounced independently of the JNI code.

                  I did look around for a way to unload native libs so I could load in init() and unload in destroy() - but could not find one....

                  Perhaps I missed something ?

                  Jules

                  • 6. Re: JNI Libraries, Classloaders and redeployment
                    user57

                    Try putting your classes that use JNI into lib/ext (2.x) or lib/ (3.x). This will allow your servlets & other components to see the classes, but not have problems realoading.

                    If you want to reload the classes which load JNI libraries then you are hosed. If this is the case consider rearchitecting to put JNI into seperate class library which can be detached from the app, thus allowing it to live in lib/ext or lib/.

                    --jason

                    • 7. Re: JNI Libraries, Classloaders and redeployment
                      redsolo

                      Another question about MBeans. Can all users add MBeans to the JBoss ? Since we're only a user among several other user at our web host, the host admins wont let any user do any special things that isnt allowed outside autoconfiguring.

                      Im new to MBeans and Beans (as well), is there any good tutorial on how to write a small bean that wraps the JNI code, and also how to call it ??

                      Does the MBean have to exist when the server is started, or can it be added later (only once) ?

                      THanks

                      • 8. Re: JNI Libraries, Classloaders and redeployment
                        user57

                        Try the JMX Book that Juha wrote, it is very informative & helpful.

                        --jason