无法在Web应用程序

时间:2017-05-23 12:59:08

标签: java c++ maven shared-libraries native

我使用GDAL库java作为具有Maven依赖性的Java Web应用程序中的依赖项:

    <dependency>
        <groupId>org.gdal</groupId>
        <artifactId>gdal</artifactId>
        <version>1.11.1</version>
    </dependency>

它包含一个jar文件并在C ++下运行本机代码,我必须从Centos中的软件包安装它。所有这些共享库都安装在

ls -l /usr/lib/java/gdal/
total 1380
-rwxr-xr-x 3 root root  23288 Feb 14  2016 libgdalconstjni.so
-rwxr-xr-x 3 root root  23288 Feb 14  2016 libgdalconstjni.so.1
-rwxr-xr-x 3 root root  23288 Feb 14  2016 libgdalconstjni.so.1.18.4
-rwxr-xr-x 3 root root 226696 Feb 14  2016 libgdaljni.so
-rwxr-xr-x 3 root root 226696 Feb 14  2016 libgdaljni.so.1
-rwxr-xr-x 3 root root 226696 Feb 14  2016 libgdaljni.so.1.18.4
-rwxr-xr-x 3 root root 136760 Feb 14  2016 libogrjni.so
-rwxr-xr-x 3 root root 136760 Feb 14  2016 libogrjni.so.1
-rwxr-xr-x 3 root root 136760 Feb 14  2016 libogrjni.so.1.18.4
-rwxr-xr-x 3 root root  73816 Feb 14  2016 libosrjni.so
-rwxr-xr-x 3 root root  73816 Feb 14  2016 libosrjni.so.1
-rwxr-xr-x 3 root root  73816 Feb 14  2016 libosrjni.so.1.18.4

但是,Web应用程序无法链接,因为链接库存在问题,尽管我从Java的类加载器中可以看到:

System.out.println(System.getProperty("java.library.path"));
/opt/jdk1.8.0_25/jre/lib/amd64:/opt/jdk1.8.0_25/jre/lib/i386::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

包含/ usr / lib文件夹。

当我调用GDAL API时来自GDAL java的错误(如果我没有调用GDAL的方法它没有错误,所以其他方法仍然有效,Web应用程序可以正常运行)是:

Caused by: java.lang.UnsatisfiedLinkError: org.gdal.osr.osrJNI.new_SpatialReference__SWIG_1()J
at org.gdal.osr.osrJNI.new_SpatialReference__SWIG_1(Native Method) ~[gdal-1.11.1.jar:na]
at org.gdal.osr.SpatialReference.<init>(SpatialReference.java:117) ~[gdal-1.11.1.jar:na]

我不知道如何解决这个问题,试图找到大量资源但没有帮助,因为我想将其部署为Tomcat服务器中的Web应用程序。在使用JVM选项

的NetBeans IDE中进行设置时,我可以使其工作
-Djava.library.path="/usr/lib/java/gdal/"

但我知道这不是Web应用程序可以运行的。

1 个答案:

答案 0 :(得分:0)

我可以在此博客http://fahdshariff.blogspot.de/2011/08/changing-java-library-path-at-runtime.html

的帮助下做到这一点
Option 2: Add path to usr_paths
Instead of having to re-evaluate the entire java.library.path and sun.boot.library.path as in Option 1, you can instead append your path to the usr_paths array. This is shown in the following code:
/**
* Adds the specified path to the java library path
*
* @param pathToAdd the path to add
* @throws Exception
*/
public static void addLibraryPath(String pathToAdd) throws Exception{
    final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    //get array of paths
    final String[] paths = (String[])usrPathsField.get(null);

    //check if the path to add is already present
    for(String path : paths) {
        if(path.equals(pathToAdd)) {
            return;
        }
    }

    //add the new path
    final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length-1] = pathToAdd;
    usrPathsField.set(null, newPaths);
}