Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove load library calls #15

Closed

Conversation

pmauduit
Copy link

The main idea behind this PR is to avoid calls to System.loadLibrary()

In some special but common cases, several different calls to System.loadLibrary using the same lib name as argument can lead to undesired behaviours (Raising an exception "java.lang.UnsatisfiedLinkError: library already loaded in classloader").

A typical case of specific classloading is when using the library in several different webapps nested into the same servlet container (tomcat). Even if the library is actually correctly configured and fully available, GeoTools reports it as unavailable (due to the exception thrown).

A PoC which tends to prove this issue has been provided here: https://github.com/pmauduit/jai-imageio-sample/tree/master/jai-imageio-gdal-sample-webapp

In this PoC webapp, a first approach makes use of the JNI GDAL/OGR bindings directly, another one is then attempted using GeoTools (class JniOGRDataStoreFactory). The first one (once tomcat is correctly configured) shows that the library is fully available, though it is reported as unavailable by GeoTools. Duplicating the webapp (so that we can have several contexts trying to use the same native lib) shows that the second one is also unable to access the features that GDAL provides.

The litterature I could read on the topic (classloading in Java seems to be one of the big mistery around the language, though) explains that we should avoid packaging webapps with jar files making use of the System.loadLibrary calls. These have to be shared: the jar file should be made available in the higher-level classloader (for tomcat under debian-like distros with libgdal-java installed, it means having a symlink from ${CATALINA_HOME}/lib/gdal.jar pointing to /usr/share/java/gdal.jar).

In some special but common cases, several different calls to
System.loadLibrary using the same lib name as argument can lead to
undesired behaviours (Raising an exception
"java.lang.UnsatisfiedLinkError: library already loaded in classloader").

A typical case of specific classloading is when using the library in
several different webapps nested into the same servlet container
(tomcat). Even if the library is actually correctly configured and fully
available, GeoTools reports it as unavailable (due to the exception
thrown).

A PoC which tends to prove this issue has been provided here:
https://github.com/pmauduit/jai-imageio-sample/tree/master/jai-imageio-gdal-sample-webapp

In the PoC webapp, a first approach makes use of the JNI GDAL/OGR
bindings directly, another one is then attempted using GeoTools (class
JniOGRDataStoreFactory). The first one (once tomcat is correctly
configured) shows that the library is fully available, though it is
reported as unavailable by GeoTools.

The litterature I could read on the topic (classloading in Java seems to
be one of the big mistery around the language, though) explains that we
should avoid packaging webapps with jar files making use of the
System.loadLibrary calls. These have to be shared: the jar file should
be made available in the higher-level classloader (for tomcat under
debian-like distros with libgdal-java installed, it means having a
symlink from ${CATALINA_HOME}/lib/gdal.jar pointing to
/usr/share/java/gdal.jar).

Note: There are several other calls to System.loadLibrary(), but they
are made from the ImageIO-Ext code, and then should be fixed there.
- Need to use my specific version of imageio-ext (which suppresses the
  uneeded System.loadLibrary calls)
- Adding the local maven repository
@pmauduit
Copy link
Author

Sorry for the noise, wrong repository.

@pmauduit pmauduit closed this Apr 29, 2014
@fvanderbiest
Copy link

For the record, the good one is geotools#443

nstires-boundless pushed a commit that referenced this pull request Apr 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants