How to add a native library in Tomcat?

Solution 1:

The accepted answer (as of Feb 2016) is just plain wrong.

  • You are never supposed to edit catalina.bat / catalina.sh. Don't ! (The only file in Tomcat's bin/ dir that you are supposed to touch is setenv.bat).

  • The right config variable is CATALINA_OPTS, not JAVA_OPTS.

  • If you are on Windows then you don't want to quote the value for the SET command as the quotes become part the actual value. (unlike on Unix/Linux)

  • It is likely you'll want to retain what is already in java.library.path.

(in the following I'll assume you are on Windows, change accordingly for Linux/Solaris/Mac OSX).

Here's how to do it: Put a file called setenv.bat into the same directory as catalina.bat. The file will not exist, unless you've created it yourself previously. So create the file. It must have the following content for your purpose:

set CATALINA_OPTS=%CATALINA_OPTS% -Djava.library.path=%PATH%;c:\mydlls

On Windows java.library.path will default to %PATH% so an alternative route to all of the above would have been to change your PATH environment variable.

If you do not want to have confusion over exactly from where the JVM will load your native libraries then omit the %PATH%; part from the above. Personally I omit %PATH% for this reason but that is a matter of taste.

Solution 2:

It has to be setup in catalina.bat instead of startup.bat.

set JAVA_OPTS="-Djava.library.path=/usr/tomcat/shared/lib"

can be put after

:noJuliManager
set JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%

Solution 3:

According to the comments on catalina.bat, I think the right place is CATALINA_OPTS.

rem   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
rem                   "run" or "debug" command is executed.
rem                   Include here and not in JAVA_OPTS all options, that should
rem                   only be used by Tomcat itself, not by the stop process,
rem                   the version command etc.
rem                   Examples are heap size, GC logging, JMX ports etc.