Loading a shared native library in Tomcat on Ubuntu

試著忘記壹切 提交于 2019-12-10 10:39:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!