How to include native library (JNI) in play 2.1 RC

2019-05-23 18:54发布

问题:

I am using Play Framework 2.1 RC2 to build an application that utilizes TFS Java SDK. TFS APIs require a set of native dlls (e.g. native_auth.dll) to be included.

How do I make the native libraries available when the application is running? Where can I specify the native dlls to be included?

Update: I tried static block System.load("full path") and loaded it using app.classloader().loadClass("NativeUtils.class"); , but still I get the exception:

java.lang.UnsatisfiedLinkError: com.microsoft.tfs.jni.internal.platformmisc.NativePlatformMisc.nativeGetEnvironmentVariable(Ljava/lang/String;)Ljava/lang/String; at com.microsoft.tfs.jni.internal.platformmisc.NativePlatformMisc.nativeGetEnvironmentVariable(Native Method) at com.microsoft.tfs.jni.internal.platformmisc.NativePlatformMisc.getEnvironmentVariable(NativePlatformMisc.java:134) at com.microsoft.tfs.jni.PlatformMiscUtils.getEnvironmentVariable(PlatformMiscUtils.java:52) at com.microsoft.tfs.core.config.httpclient.DefaultHTTPClientFactory.shouldAcceptUntrustedCertificates(DefaultHTTPClientFactory.java:288) at com.microsoft.tfs.core.config.httpclient.DefaultHTTPClientFactory.configureClientParams(DefaultHTTPClientFactory.java:324) at com.microsoft.tfs.core.config.httpclient.DefaultHTTPClientFactory.newHTTPClient(DefaultHTTPClientFactory.java:137) at com.microsoft.tfs.core.TFSConnection.getHTTPClient(TFSConnection.java:1041) at com.microsoft.tfs.core.TFSConnection.getWebService(TFSConnection.java:874) at com.microsoft.tfs.core.config.client.DefaultClientFactory$9.newClient(DefaultClientFactory.java:265) at com.microsoft.tfs.core.config.client.DefaultClientFactory.newClient(DefaultClientFactory.java:90) at com.microsoft.tfs.core.TFSConnection.getClient(TFSConnection.java:1470) at com.microsoft.tfs.core.TFSTeamProjectCollection.getWorkItemClient(TFSTeamProjectCollection.java:370)

Thanks in advance for your inputs!

回答1:

When using System.loadLibrary, the only thing we specify is the name of the DLL file we want. The JVM will search for it in the "Java library path." This is a path which is given by the java.library.path system property (and hence can be altered on the java.exe command line using the -D option). The default value of this appears to be related to the Windows path, though it appears to be somewhat scrambled, and I'm not quite sure how or why. In other words, I'm not sure how the Windows JVM creates the initial value of java.library.path.

So, you should simply speciry -Djava.library.path=<path to your dlls> when running your application. More details on that is here.



回答2:

I had the same problem with OpenCV library. I found the solution here: http://answers.opencv.org/question/16689/jni-error-on-playframework-v211/

You MUST run your application with "play start" command, not "play run".

"play run" command starts your application in development mode and "play start" command starts in production mode. I don't know every difference between them but one obvious thing is ,

Only when we use "play start", a new JVM for you application is launched and it loads native libraries you specified by System.load(...)

If you run your play application with "play start" command it will work. Unfortunately "play run" does not load libs.



回答3:

I didn't find a solution to a problem, else than running play start, and running it as play start while development sucks, so I've created an issue: https://github.com/playframework/playframework/issues/2212 . If you encounter the problem, you can add there a comment, describing the situation. Adding there a comment rases it upper in issue list. Upper the issue is, higher probability, that it'll be fixed.