Tomcat can't work well with Java Class that loads two shared libraries?

Dear All,

here is my environment:

Fedora Core 6/Tomcat 5.5.23/JRE 1.5.0_11_b03.

My Goal is to develop a web service to connect to a legacy DB with a C library.

There is a shared library a.so that contains C APIs to connect to that legacy DB.

so I decide to use JNI to access this DB through its shared library a.so. here is what I did:

1. create an C file that contains functions that will call the APIs in a.so to access the DB. Of course, these C functions will implement the .h header files generated by javah.

2. since these functions use the APIs in a.so, so I compile this C file(AMSLibCalls.c) with

a. gcc -fPIC -c -I/opt/jdk1.5.0_11/include -I/opt/jdk1.5.0_11/include/linux -Wall AMSLibCalls.c

b. gcc -shared -I/opt/jdk1.5.0_11/include -I/opt/jdk1.5.0_11/include/linux -Wl,-soname,libAMSLibCalls.so.1 -o libAMSLibCalls.so AMSLibCalls.o ./a.so -lc

to generate another shared library libAMSLibCalls.so.

3. since shared lib libAMSLibCalls.so will call the shared lib a.so, so in the Java class AMSJavaAPI.java, I load both of these two shared libs:

static{

System.load("/usr/src/ams/a.so");

System.load("/usr/src/ams/libAMSLibCalls.so");

}

4. everything works fine if I don't put this AMSJavaAPI class in Tomcat.

5. if I put this class in tomcat and use a jsp page to call it, it will always give me the NoClassDefFound exception. I looked at the exception, it seems when the lib libAMSLibCalls.so is loaded, it searchs the a.so too and can't find it.

does anybody have clue what possibly goes wrong?

Thanks,

Alex Hu

[1657 byte] By [AlexHua] at [2007-11-27 4:08:03]
# 1

> 4. everything works fine if I don't put this

> AMSJavaAPI class in Tomcat.

>

You mean that it loads both libraries if it isn't in Tomcat?

If yes, then it is one of the following.

1. The paths are not correct.

2. It is permissions problem

3. There is another dependency by a.so which can't be loaded due to it is not found or permissions.

Note that often printing the stack trace can give more information.

jschella at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Thank you! jschell.

the problem is the exact same class, if I put it outside of tomcat, say, a non-web java application, it works just fine. so the path should be right because I use the absolute path.

regarding the permission, could you give me more explanation about it? why permission will cause problem in Tomcat?

I'm sorry I'm out for trainning, late today I will post the stack trace for you.

Thanks again.

Alex

AlexHua at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
> regarding the permission, could you give me more> explanation about it? why permission will cause> problem in Tomcat?Tomcat runs as a user. If that user does not have read permission on a file/directory then it can't access it.
jschella at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Thank you for the explanation! jschell,

I did chmod all related files to 777 and do the test again, it turns out no go. here is the stack trace:

INFO: Server startup in 4481 ms

- Servlet.service() for servlet AxisServlet threw exception

java.lang.NoClassDefFoundError

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:242)

at org.apache.axis.utils.ClassUtils$2.run(ClassUtils.java:177)

at java.security.AccessController.doPrivileged(Native Method)

at org.apache.axis.utils.ClassUtils.loadClass(ClassUtils.java:160)

at org.apache.axis.utils.ClassUtils.forName(ClassUtils.java:142)

at org.apache.axis.utils.cache.ClassCache.lookup(ClassCache.java:85)

at org.apache.axis.providers.java.JavaProvider.getServiceClass(JavaProvider.java:428)

at org.apache.axis.providers.java.JavaProvider.initServiceDesc(JavaProvider.java:461)

at org.apache.axis.handlers.soap.SOAPService.getInitializedServiceDesc(SOAPService.java:286)

at org.apache.axis.deployment.wsdd.WSDDService.makeNewInstance(WSDDService.java:500)

at org.apache.axis.deployment.wsdd.WSDDDeployment.getDeployedServices(WSDDDeployment.java:503)

at org.apache.axis.configuration.FileProvider.getDeployedServices(FileProvider.java:296)

at org.apache.axis.transport.http.AxisServlet.reportAvailableServices(AxisServlet.java:482)

at org.apache.axis.transport.http.AxisServlet.doGet(AxisServlet.java:260)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)

at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)

at java.lang.Thread.run(Thread.java:595)

BTW, I logged in as root, so all the classes' owners are root. I put AMSJavaAPI class as a web service class using Axis Soap engine. this is why your see the axis in the stack trace.

Gosh, really have no clue what happened.

-AH

AlexHua at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

It is your environment so I can't really help you much with it.

I gave you the reasons above. Note that 'paths' refers both the java class paths and shared library paths. Permissions refers to directories and not just files.

Other than that if you ran the service as root (and you should verify that it when it came up it was still root) then that would suggest that it wasn't a permissions problem.

jschella at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Hi, jschell,from the stack trace, at least you can tell that it's not the permission isssue, instead, it's path issue, right? I don't know in tomcat, how Tomcat loads those native libraries, which search paths it's using? by setting LD_LIBRARY_PATH environment variable in
AlexHua at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

> Hi, jschell,

>

> from the stack trace, at least you can tell that it's

> not the permission isssue, instead, it's path issue,

> right?

Not really.

You can comment out the load library call and determine whether the java itself loads.

If it does then it is a shared library problem. The exception for that should give more info.

jschella at 2007-7-12 9:13:23 > top of Java-index,Java HotSpot Virtual Machine,Specifications...