简体   繁体   中英

Set java.library.path for tomcat

I am currently trying to get a Tomcat-Application running with Maven with a native library, which needs a specified java.library.path. Normaly, for running Tomcat with the path, one has to do things like adding the path to Tomcat: How to add a native library in Tomcat? .

But in this case, I want to run Tomcat with the Maven plugin, and unfortunately, I couldn't find any hint on how to set java.library.path for the Tomcat-Maven plugin. Just setting argLine with -Djava.library.path=XX does not work. Does anybody have an idea how to include native libraries with the Tomcat-Maven plugin?

One way that should work according to https://groups.google.com/forum/#!msg/maven-nar/1mz9oWj-65U/dbQEK_6DLdYJ is setting the path with System.setProperty("java.library.path", javaLibPath); and afterwards let the classloader reload this with

Field field = ClassLoader.class.getDeclaredField("sys_paths");
field.setAccessible(true);
field.set(null, null);

Unfortunately, for me this does not work, the error is thrown anyway. (This hint is also given by http://blog.cedarsoft.com/2010/11/setting-java-library-path-programmatically/ ).

Another way (not to solve the real problem, but to workaround that I don't know a way to load set java.library.path in the Tomcat-Maven plugin) seems to be to add the library during the programs run, like described for example here: Directory separator should not appear in library name: Macintosh HD/Users/sakkisetty/Documents/dll/FasExtend.dll . Unfortunately, this does not work either.

           File f2 = new File("/home/my/absolute/path");
    for (File f : f2.listFiles()){
        System.out.println("Datei: " + f.getAbsolutePath());
        if (f.isFile() && f.getName().endsWith(".so")){
            System.out.println("Loading: " + f.getAbsolutePath());
            // System.loadLibrary(f.getName());
            System.load(f.getAbsolutePath());
        }
    }

There are two possible results of this: If I try it like this, the same errors are thrown as if the libraries aren't loaded at all. If I use System.loadLibrary , it does not find the library because the library isn't in the java.library.path and System.loadLibrary does not take absolute paths - so all in all, this way does not work either, at least how I tried it.

I got mvn tomcat:run to work with an opencv_java249.so file by using MAVEN_OPTS .

Simply doing:

MAVEN_OPTS=-Djava.library.path=/path/to/your/native/libfolder

worked fine. I'm using an absolute path for this on my machine and it's working. I haven't tried a relative one though.

You can set java.library.path for the tomcat maven plugin in your pom.xml like this:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <systemProperties>
            <java.library.path>${basedir}/libraryfolder</java.library.path>
        </systemProperties>
    </configuration>
</plugin>

One possible solution to this problem was for me to set the java.library.path programatically in another way (from Setting Djava.library.path programmatically (or alternatives)? ):

private void addJNIPath(String pathToAdd) {
    try {
        Field usrPathsField = ClassLoader.class
                .getDeclaredField("usr_paths");
        usrPathsField.setAccessible(true);

        String[] paths = (String[]) usrPathsField.get(null);

        for (String path : paths)
            if (path.equals(pathToAdd))
                return;

        String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
        newPaths[newPaths.length - 1] = pathToAdd;
        usrPathsField.set(null, newPaths);
    } catch (NoSuchFieldException | SecurityException
            | IllegalArgumentException | IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

As this still does not seem to be an realy good solutions (as it should be possible to set the path somehow via tomcat), I would be happy to hear about other solutions to the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM