Search code examples
javaweb-servicesjava-native-interfaceshared-librariestomcat8

Using JNI so files on a dynamic web project(Apache Tomcat Server)


I am making a dynamic web project, which requires to use an external jar file. That jar uses several JNI so files. Now I have the jar and all the required so files. I have added the jar to the java build path, but I am not sure how to introduce the so files to the tomcat server.

I tried adding path to the so file as "LD_LIBRARY_PATH":

(Properties->Run/Debug Settings->edit->Environment->Select) and also adding the path to "java.library.path"(Run As->RunConfigurations->Tomcat v8 Server->Arguments->VM arguments) flag.

Still it is not working, I am getting below error, whenever the my code is trying to access functions/classes from the above mentioned jar:

SEVERE: Servlet.service() for servlet [org.iotivity.simulator.agent.LaunchSimulator] in context with path [/SimulatorAgent] threw exception [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: org.oic.simulator.server.SimulatorResource$Type at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) at org.iotivity.simulator.agent.LaunchSimulator.doPost(LaunchSimulator.java:86) at org.iotivity.simulator.agent.LaunchSimulator.doGet(LaunchSimulator.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

I have tried the same code with a simple Java Application, and it works fine by setting the LD_LIBRARY_PATH variable.

Any suggestion will be highly appreciated.


Solution

  • Your error is a ClassNotFoundException, which would suggest that the JAR isn't properly included. If there was a problem with the native library, you'd get a UnsatisfiedLinkError instead. To make the JAR available to Tomcat, you'd need to add it to the classpath (-classpath=/path/to/your.jar) or include it in your WAR. The latter depends on your build system, but if you use Maven it's rather easy using the maven-shade-plugin (this only works for the JAR though, the native code you'll have to keep on distributing separately). Hope that helps!