问题
How do I load a shared library in Tomcat6, on Ubuntu?
I have created a library "awragrids" with name "libawragrids.so" and placed it in
/var/lib/tomcat6/shared
I have set the following environment variables in the terminal from which I call start tomcat:
export PATH=$PATH:/var/lib/tomcat6/shared
export JAVA_OPTS="-Djava.library.path=/var/lib/tomcat6/shared"
export LD_LIBRARY_PATH=/var/lib/tomcat6/shared
I then try and load the library with
private static final String GRIDTOOL_LIBRARY_NAME = "awratools";
static {
try {
System.loadLibrary(GRIDTOOL_LIBRARY_NAME);
} catch (java.lang.SecurityException e) {
System.out.println("Not allowed to load dynamic library " + GRIDTOOL_LIBRARY_NAME + ".");
throw e;
} catch (java.lang.UnsatisfiedLinkError e) {
System.out.println("Failed to load dynamic library " + GRIDTOOL_LIBRARY_NAME + ".");
throw e;
}
}
and wind up in the second catch block. All the reading I've done suggests that either of the first two exports should do the trick. On windows, I put "awragrids.dll" in a folder on the path and it works fine.
I have spent way too much time on this and really need to get some sleep... please help!
回答1:
Make sure that Tomcat's startup script--catalina.sh in /var/lib/tomcat6/bin--is actually using the JAVA_OPTS from the environment. In the script on the system I'm looking at now, someone put in a complete definition without pulling in the environment setting.
回答2:
I think this may have been a mistake on my part. When I originally compiled the library, it had a different name ("libawargrids.so"), so I renamed it with:
mv libawragrids.so libawratools.so
While the equivalent seems to work on Windows, perhaps it doesn't work on Linux. When I compiled it again using the name "awratools" (producing "libawratools.so), the problem went away.
While the problem is now fixed, if someone could confirm that the renaming is probably what caused it (and perhaps explain why), that would be appreciated.
Sorry for wasting peoples time but thanks for the help!
P.S. I have now placed the lines:
export JAVA_OPTS="-Djava.library.path=/var/lib/tomcat7/shared"
export LD_LIBRARY_PATH=/var/lib/tomcat7/shared
at the top of /etc/init.d/tomcat6, so it does not matter which user starts the service. Please let me know if this is, for some reason, bad.
来源:https://stackoverflow.com/questions/10773390/loading-a-shared-native-library-in-tomcat-on-ubuntu