Search code examples
javaeclipseweb-servicestomcatcatalina

Does Tomcat (as a Service) not load native libraries for a Java web application?


I am using Tomcat7 and Ubuntu. I have a Java web application which uses some native libraries. When I run the web application within Eclipse it works through Eclipse internal Tomcat server during debugging. However, when I deploy the applcation to a hosted Tomcat service, the application fails when it reaches the point of loading these libraries.

  1. I put the native libraries in /home/me/my_shared_libs, and gave
    folder and file ownership to user "Tomcat7" -- sudo chown
  2. I give all permissions of the native libraries to "Tomcat7" user -- sudo chmod
  3. In do sudo vi /usr/share/tomcat7/bin/setenv.sh, and put the following in the file export CATALINA_OPTS="-Djava.library.path=/home/me/my_shared_libs"
  4. Then I restart Tomcat -- sudo service tomcat7 restart And, whenever refernce to load native libraries is reached, I get an error about InvocationTargetException.

I am also open to the option of adding the native libraries as part of the the application's .WAR file. (Although I am not sure how to do this in Eclipse).

Log of /var/log/tomcat7/catalina.out-->

Jun 30, 2016 8:11:50 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3643 ms
Load my_native_lib_called. libmachoman.so: cannot open shared object file: No such file or directory

EDIT: I found out something very interesting. Tomcat does pick up the library location that I set above. What happens is I have two types of libraries (.so) files in the location. The first library (libcore.so) calls/loads the the second library (libmachoman.so). libcore.so is found and loaded both libmachoman.so is not, even though both are in the same location.


Solution

  • After days of headache, I have a solution.

    1. Edit file

      sudo vi /etc/ld.so.conf

    2. Append the location of native libs in file

      include /etc/ld.so.conf.d/*.conf /home/me/my_shared_lib

    3. load config

      sudo ldconfig

    4. View the new change

      ldconfig -p | grep my_shared_lib

    This tells the dynamic linker where to look for native libraries.

    My problem is solved.

    There are other alternative solutions here, which may or may not have some cons.

    Alternatively (also found out), you can can export LD_LIBRARY_PATH in the setenv.sh file under /usr/share/tomcat7/bin/ instead of the above steps. Settings become part of Tomcat; cleaner approach.