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

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");
jgdvishnuSoftware ArchitectAsked:
Who is Participating?
jgdvishnuConnect With a Mentor Software ArchitectAuthor Commented:
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
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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?
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.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

jgdvishnuSoftware ArchitectAuthor Commented:
@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.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
jgdvishnuSoftware ArchitectAuthor Commented:
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

mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
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.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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)
girionisConnect With a Mentor Commented:

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.
jgdvishnuSoftware ArchitectAuthor Commented:
@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.
jgdvishnuSoftware ArchitectAuthor Commented:
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.
All Courses

From novice to tech pro — start learning today.