Set java.library.path for testing

You can add system properties to the maven-surefire-plugin when the tests are running with the help of the systemPropertyVariables attribute:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <systemPropertyVariables>
      <propertyName>java.library.path</propertyName>
      <buildDirectory>/usr/local/lib</buildDirectory>
    </systemPropertyVariables>
  </configuration>
</plugin>

This will add the java.library.path as a system property when the tests are ran. The modification you are making is not taken into account since the tests are ran in a forked VM.


You are most likely encountering this problem because you are using a Maven plugin like surefire or failsafe which launches a new JVM to run your tests and your launch configuration is not getting passed on. Also, you probably also need to set the 'java.library.path' on the command line of the new process so that the native library and all of its dependencies can be linked at startup. If you use 'systemPropertyVariables' it won't have the same effect, but might work if you lucky. Here is an example plugin configuration that is working for me:

        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19</version>
            <executions>
                <execution>
                    <id>my-external-tests</id>
                    <goals>
                        ...
                    </goals>
                    <configuration>
                        <argLine>-Djava.library.path=/usr/local/lib</argLine>
                        <groups>com.myCompany.ExternalTest</groups>
                        <includes>
                            <include>**/*Suite.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>