File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Tomcat and dlls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat and dlls" Watch "Tomcat and dlls" New topic
Author

Tomcat and dlls

Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Hi All,

I have a webapp that uses dlls and I have two questions about this.

1. Is there anyway to put the dlls in the war and have Tomcat find them. BTW there are 8 dlls, and one uses the others. For the moment I have the dlls in the bin of Tomcat (or on the path), and that works OK.

2. If a wabapp fails then usually you can restart it in Tomcat without restarting Tomcat. BUT when I restart my webapp, it tries to reload the dlls and Tomcat complains that they are already there. How can I unload the dlls without restarting Tomcat?

Maybe these two questions are related, if I manage to put the dlls in the war, Tomcat would unload them with the app? Or am I just being hopeful.

For the time being I am using Tomcat 5.5 but could change to Tomcat 6 if these questions whould be easier to answer. And I certainly will if I am the lucky winner of the SUPER new book "Professional Apache Tomcat 6". Oooh pick me, pick me!

Thanks in advance for any help

Nicole

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Nichole,

It looks, from the release notes, that this will be no different in version 6 than in prior versions.

I don't think it matters (to Tomcat, and Java) where you put the actual DLLs but the Java classes that load them should be under Tomcat/shared.

From:
http://download.nextag.com/apache/tomcat/tomcat-6/v6.0.14/RELEASE-NOTES

=======================
JNI Based Applications:
=======================
Applications that require native libraries must ensure that the libraries have
been loaded prior to use. Typically, this is done with a call like:

static {
System.loadLibrary("path-to-library-file");
}

in some class. However, the application must also ensure that the library is
not loaded more than once. If the above code were placed in a class inside
the web application (i.e. under /WEB-INF/classes or /WEB-INF/lib), and the
application were reloaded, the loadLibrary() call would be attempted a second
time.

To avoid this problem, place classes that load native libraries outside of the
web application, and ensure that the loadLibrary() call is executed only once
during the lifetime of a particular JVM.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Thanks Ben,

I am working on it. First of all I noticed that in an normal app (not Tomcat) I can call System.loadLibrary() serveral times in a row with the same dll with no exceptions or complaints. I haven't yet tested under Tomcat. Also you said (or quoted)

To avoid this problem, place classes that load native libraries outside of the web application, and ensure that the loadLibrary() call is executed only once during the lifetime of a particular JVM.


How?

Nicole
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not a JNI expert but I can tell you what I'm doing.

We use a third party middleware library that consists of one DLL and one Jar file. If we have more than one copy of our app running and we put the jar file in the WEB-INF/lib directory of each app, the first app to use it locks all the others out.
To fix this we've placed the jar file under tomcat/shared/lib (we're using Tomcat 5.5 right now).
With the jar file in that location all the applications can access the middleware without a problem.

I'm assuming there is a static reference of some sort to the objects that make the JNI connections that only gets loaded when the first request for it comes in (like in the sample from the release notes above). Since the classloaders for the tomcat/shared/lib and the tomcat/common/lib directories are outside of our application, restarting the app, doesn't affect them.

Does this help?
[ October 03, 2007: Message edited by: Ben Souther ]
Alex Deygan
Greenhorn

Joined: Sep 22, 2014
Posts: 2

Follow this link to unload dll:

http://www.codethesis.com/blog/unload-java-jni-dll

I tried it with my web application running on Tomcat, and it works!

Briefly, I call System.loadLibrary from the class which is loaded by my custom class loader using reflection. This class is then removed by garbage collector.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat and dlls