15

I want to add gdal library in Tomcat. I read Native libraries not found in Tomcat but don't understand where in startup.bat I should add -Djava.library.path.

Errors:

exception

javax.servlet.ServletException: Servlet execution threw an exception
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:70)

root cause

java.lang.UnsatisfiedLinkError: org.gdal.ogr.ogrJNI.GetDriverCount()I
org.gdal.ogr.ogrJNI.GetDriverCount(Native Method)
org.gdal.ogr.ogr.GetDriverCount(ogr.java:98)
org.geotools.data.ogr.OGRDataStore.<clinit>(OGRDataStore.java:169)
test.Read.getKadnum(Read.java:56)
test.Zipper.mifUnzip(Zipper.java:139)
test.Zipper.Unzip(Zipper.java:60)
test.uploadfile.doPost(uploadfile.java:105)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:70)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.

I downloaded gdal 64bit from: http://vbkto.dyndns.org:1280/sdk/PackageList.aspx?file=release-1600-x64-gdal-1-9-mapserver-6-2.zip

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Kliver Max
  • 5,107
  • 22
  • 95
  • 148

3 Answers3

38

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.

peterh
  • 18,404
  • 12
  • 87
  • 115
  • `setenv.bat` is absence in a default TC8.5 ZIP. Please create it in `bin` - and read `RUNNING.txt` ch 3.4 – Sebastian J. Aug 02 '17 at 13:54
  • 1
    In linux / osx create "setenv.sh" in the bin folder and add line: export CATALINA_OPTS="$CATALINA_OPTS -Djava.library.path=.." – ormurin Jun 17 '19 at 12:40
10

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%
coderLMN
  • 3,076
  • 1
  • 21
  • 26
  • It would be good to explain which file to modify, it looks like OP is starting in the Java world. – Luiggi Mendoza Dec 24 '12 at 08:18
  • cant find this line `:noJuliManager` in startup.bat. – Kliver Max Dec 24 '12 at 08:20
  • Thanks now try to do this. – Kliver Max Dec 24 '12 at 08:26
  • @Jinzhao Wu its not help( I use : `:noJuliManager set JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER% set JAVA_OPTS="-Djava.library.path=D:\liferay-portal-6.1.0-ce-ga1\tomcat-7.0.23\shared\lib\gdal\java"` – Kliver Max Dec 24 '12 at 08:53
  • @KliverMax what's in your log? I find that there is no 64-bit gdal library for windows platform yet(see http://java.net/jira/browse/IMAGEIO_EXT-39?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aworklog-tabpanel). – coderLMN Dec 24 '12 at 08:59
  • 2
    @KliverMax From the exception like `org.gdal.ogr.ogr.GetDriverCount(ogr.java:98)` you can see that the lib file has been loaded successfully. I think this exception is about the usage of the lib file itself, which I'm not familiar with. You might want to initiate another question about gdal library. – coderLMN Dec 24 '12 at 09:49
  • @KliverMax: That is good information. When the server restarts, does it try to load this library again? What and where to configure to load the library only once? OR unload when the server is stopped and load during start!! – Gana Oct 06 '14 at 12:31
  • 2
    `set JAVA_OPTS="-Djava.library.path=%CATALINA_BASE%\lib"` I used this rather, is it right? –  Aug 25 '16 at 08:39
  • 2
    As is said by @peterh below, you should never edit catalina.bat/.sh. This should be in setenv.bat. – Jamie Oct 02 '17 at 22:16
6

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.
Francesco
  • 61
  • 1
  • 1
  • 1
    This is correct, it's `CATALINA_OPTS`, not `JAVA_OPTS` (although technically the latter will work as well). But indeed there are many [other problems with accepted answer](http://stackoverflow.com/a/35552972/1504556). – peterh Feb 22 '16 at 11:57