To Unload a shared object in web application what should be path that should be passed in JSP or in business delegate class

Posted on 2014-04-02
Last Modified: 2014-04-15
I have a web application using native methods. I want to know approach on how to unload the shared object.

I followed This example is good for standalone.

In my web application I am intending to set the classloader and all classes and methods to null before running System.gc().

what should be the path of Java class in web application if the native method is called in a class that is called by JSP.

Class ca = cl.findClass("com.codethesis.example.A");
Question by:jgdvishnu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
LVL 36

Expert Comment

ID: 39973952
You won't be able to do that in w web application because the class loading is handled by the servlet container (ie. tomcat, etc). And because the JSP's compiled class is loaded by the servlet container (using it's classloader) the class that uses the native library will also be loaded using that classloader.

I can't think of any other way to do it either. What is the requirement for wanting the native library to be unloaded?
LVL 35

Expert Comment

ID: 39974262
How do you load the dll in your web application? If you use the default classloader then mccarl is right, you can't do it (unless you gc the web application's classloader, but this would mean that you don't have a web application any more). If you use your own classloader then the approach described on the link should work for a web application as well.

Author Comment

ID: 39976163
@mccarl  when I restart the application and hit the first request for JSP, time the System has already the library loaded and it gives error

Unable to load library due to: Native Library /usr/sap/ already loaded in another classloader

@girionis here is the logic...
// Loading through default class loader does not work
       static {
              try {
              } catch (java.lang.UnsatisfiedLinkError exception) {
              } finally {

Open in new window

Using custom class loader executed through different JSP to unload. It gives error that class not found which is being hit by another jsp on same application.

	public  void testLoadCheck() throws Exception {
        CustomClassLoader cl = new CustomClassLoader();
        Class ca = cl.findClass("com.insight.FedEx");
        Object a = ca.newInstance();
        Method p = ca.getMethod("testCall");// need to implement interface for method with parameter.
        p = null;
        ca = null;
        a = null;
        cl = null;

Open in new window

I heard this link will help. trying to investigate. Please confirm how this will translate into code...
Diagnosing Web Application OutOfMemoryErrors

I will be grateful.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 36

Expert Comment

ID: 39976878
when I restart the application
Honestly, if I were you, I would spend my time elsewhere rather than trying to solve this issue. Because even if you were to fix this, it would still be my recommendation that rather than just restarting your application, I would restart the whole Tomcat (I pretty sure that's what you were using) container. It doesn't add much time at all to do a full restart compared to just restarting the webapp, but it ensures that you are always starting from a known good state.

A long time ago, I gave up on Tomcat's "feature" to be able to deploy, undeploy, start, stop, restart individual webapps within the container. It sounds good in theory, but when you actually start using it, issues like what you are seeing, and other things like memory leaks, etc start popping up. In a production environment, for me it is just too big a risk to take. The appliction may appear to be functioning normally after a "webapp restart" but you just never really know if maybe it is just a little bit closer to erroring out with a OutOfMemoryError or others. If you do a full restart of Tomcat, you can be sure that it always comes up in exactly the same state.

Having said all that, if you are still interested in getting this to work "just for the sake of it", then... One question that I had was, are you still having this class that loads the native library and the library itself in Tomcat's shared lib folder? Because if you do, it still should be loaded only once by a shared classloader that ISN'T associated with your webapp's classloader and so you shouldn't be getting the "already loaded" error.

Author Comment

ID: 39976966
I want to try on tomcat because I need to make product that eventually runs on SAP PI server(on which I have no administrative control). So I do want to make it sturdy enough that it runs on tomcat.

with Shared library it does not recognise the classes in the jar and it gives warning on server start up as shown below-

WARNING: Problem with directory [/home/adm/tomcat/apache-tomcat-6.0.39/bin/$CATALINA_HOME/shared/lib/libTest.jar:$CATALINA_HOME/shared/lib/$CATALINA_HOME/shared/lib/], exists: [false], isDirectory: [false], canRead: [false]

Open in new window

LVL 36

Assisted Solution

mccarl earned 250 total points
ID: 39976978
It appears that you possibly didn't read (didn't see) my last comment in another question of yours...

Did you fix the format of the entry that you added to the file?

Instead of using $CATALINA_HOME you need to use ${catalina.home} and also you need a comma (,) between each path entry not colon (:). Although as I also said in that other comment, you don't add the .so files to this entry, just the .jar file so you should only have one entry anyway, and no need for a comma at all.
LVL 36

Expert Comment

ID: 39976981
I want to try on tomcat because I need to make product that eventually runs on SAP PI server
And in regards to that, SAP PI probably has a totally different classloading arrangement to Tomcat so the problems that you are facing in Tomcat may not even exist in SAP (and also you might find different problems in SAP that you don't have in Tomcat)

They are different products running different environments and so using one to predict/test how your code will function on the other is not going to amount to much (at least when you are talking about low level details like classloaders)
LVL 35

Assisted Solution

girionis earned 250 total points
ID: 39977431

This piece of code will use the default system classloader (which is specific to each JVM implementation).

It gives error that class not found

In your classloader you need to specify the parent classloader that loaded the com.insight.FedEx class. If you don't specify this classloader then your CustomClassLoader should look in the path that you have the class com.insight.FedEx, otherwise you will end up with a class not found exception.

But I still don't understand what the requirement is for wanting the dll unloaded.

Author Comment

ID: 39988711
@mccarl - I did get the shared library jar to work. Thanks. I am not clear on lifecycle of Customclassloader and how it works.

@girionis the need to unload dll is to restart only one application and not the whole application server. the second time when same aplication is loaded it gives error that the dll(or shared library in UNIX) is already loaded.

Accepted Solution

jgdvishnu earned 0 total points
ID: 39992015
Instead of loading and unloading the library I just put it on shared library location as suggested by mccarl.
This does not error on Tomcat. Though this approach does not work on SAP PI server. I have posted my comments on SAP side and waiting for response.

Is there a shared library (settings) page on SAP PI server to handle native library

Author Closing Comment

ID: 40001121
Using Shared Library is better solution then loading and unloading of classes through the classloader. Through Classloader I will need to handle and force garbage collection and Garbage Collection is done by system and cannot be forced so may not work in the end.

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question