How to call native code from IntelliJ Plugin?

Answered

How example, I have a bunch of native libraries for several architectures:

mylib.dylib, mylib.so, mylib.dll

There are JNI bindings in the libs, which I want to call from the plugin itself, like this:

external fun hello(): Void

    companion object {
        init {
            System.loadLibrary("hello")
        }
    }

How example, I have a bunch of native libraries for several architectures:

mylib.dylib, mylib.so, mylib.dll

There are JNI bindings in the libs, which I want to call from the plugin itself, like this:

external fun hello(): Void

    companion object {
        init {
            System.loadLibrary("hello")
        }
    }

I want to distribute these libs among with .jar. I've tried to add binaries inside src folder and link them via Project Structure - Modules and Project Structure - Libraries with no result. Due to this issue, I can conclude that such a case should be possible. Should I add something inside build.gradle.kts? Or can I use c-interop somehow?

0
4 comments

Hi Alexandr,

Please try to place libraries in my-plugin/resources/lib directory.

1

Thanks! It worked out. But the libs are inside the .jar. I guess I need to unpack them and place in the java.library.path? I just saw some plugins which, when installed, immediately have the native libs in the Plugins folder.

0

There is no need to, the plugin class loader is able to load it from the lib directory. See:
com.intellij.ide.plugins.cl.PluginClassLoader.findLibrary()

for the details.

0

Karol Lewandowski is there an example you can share? do I need an instance of the PluginClassLoader? I am getting an exception:

java.lang.UnsatisfiedLinkError: no java_client_bridge in java.library.path: [/Users/fcoria/Library/Java/Extensions, /Library/Java/Extensions, /Network/Library/Java/Extensions, /System/Library/Java/Extensions, /usr/lib/java, .]

clearly the java.library.path is not pointing to the library, so I assume the PluginClassLoader is not being used, but I don't know why.

I am using `static { System.loadLibrary("java_client_bridge") }` 

I put the .dylib file at my-plugin/src/main/resources/lib/libjava_client_bridge.dylib

0

Please sign in to leave a comment.