Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set java.library.path for testing

One of the tests uses a native library:

System.loadLibrary("mylib");

libmylib.so is located in /usr/local/lib, So I add this directory in configuration VM options: -Djava.library.path=/usr/local/lib

However, when I run tests with Maven, this line throws UnsatisfiedLinkError:

no mylib in java.library.path

Java is invoked without this option:

/usr/lib/jvm/java-8-oracle/bin/java -Dmaven.home=/opt/idea/plugins/maven/lib/maven3 -Dclassworlds.conf=/opt/idea/plugins/maven/lib/maven3/bin/m2.conf -Didea.launcher.port=7538 -Didea.launcher.bin.path=/opt/idea/bin -Dfile.encoding=UTF-8 -classpath /opt/idea/plugins/maven/lib/maven3/boot/plexus-classworlds-2.4.jar:/opt/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=15.0.3 test

Printing System.getProperty("java.library.path") when catching the exception gives /opt/idea/bin::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib. Apparently VM options from run configuration have no effect on maven tasks.

So I tried to set library path in VM options for Maven: Settings->Build, Execution, Deployment->Build Tools->Maven->Runner->VM options. This option has the effect on java invocation command:

/usr/lib/jvm/java-8-oracle/bin/java -Djava.library.path=/usr/local/lib -Dmaven.home=/opt/idea/plugins/maven/lib/maven3 -Dclassworlds.conf=/opt/idea/plugins/maven/lib/maven3/bin/m2.conf -Didea.launcher.port=7539 -Didea.launcher.bin.path=/opt/idea/bin -Dfile.encoding=UTF-8 -classpath /opt/idea/plugins/maven/lib/maven3/boot/plexus-classworlds-2.4.jar:/opt/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=15.0.3 test

But even though Java is now invoked with this option, it still fails to load the library, and System.getProperty("java.library.path") still contains the same thing!

How to set java.library.path for tests invoked with Maven?

like image 865
Michael Ivko Avatar asked Feb 12 '16 15:02

Michael Ivko


2 Answers

As in Sachin Handiekar's comment, the issue is solved by setting LD_LIBRARY_PATH in the environment in which Idea is run. (But not in Idea settings, for some reason.)

like image 164
Michael Ivko Avatar answered Oct 08 '22 16:10

Michael Ivko


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.

like image 40
Tunaki Avatar answered Oct 08 '22 16:10

Tunaki