Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a dynamic library? #214

Closed
apahl opened this issue Apr 30, 2021 · 10 comments
Closed

How to use a dynamic library? #214

apahl opened this issue Apr 30, 2021 · 10 comments
Labels
bug Installation and functionality issues REPL involved

Comments

@apahl
Copy link

apahl commented Apr 30, 2021

Hi,
thanks a lot for this cool project!!

I would like to explore it for cheminformatics analysis and would therefore like to use the Java bindings of the RDKit toolkit.
The bindings consist of a jar artifact and a dynamic library (.so for Linux).
In the Idea IDE, I configure the program for the location of the dynamic library with this line in the build.gradle.kts:

jvmArgs = listOf("-Djava.library.path=$rdkitKnime")

with rdkitKnime being the path to the dynamic library.

Then, in the program, I call System.loadLibrary("GraphMolWrap") once, to actually load the library.

Is there a corresponding possibility to modify the java.library.path in kotlin-jupyter?

@file:Repository("file:///home/pahl/dev/github/knime-rdkit/org.rdkit.knime.bin.linux.x86_64/os/linux/x86_64")

followed by

System.loadLibrary("GraphMolWrap")

in a new cell does not work, it gives a no GraphMolWrap in java.library.path error.

Many thanks for your help.

Kind regards,
Axel

@ileasile
Copy link
Contributor

Hello! Thanks for your interest!

Does it work for you?

val rdkitKnimePath = "..."
System.setProperty("java.library.path", rdkitKnimePath)
System.loadLibrary("GraphMolWrap")

@ileasile
Copy link
Contributor

Relates to #72

@apahl
Copy link
Author

apahl commented Apr 30, 2021

Hi, thanks a lot for coming back so quickly.
Using System.SetProperty did unfortunately not work:

image

The workaround mentioned in #72 seems to work, though.
At least I now get past the System.loadLibrary call, but then calling an actual function of the package throws an error again:

image

Calling this function does work in the IDE.

@ileasile
Copy link
Contributor

Is the project you're working in IDE available in public? It would be great if I could reproduce this problem in my environment. I found the repo with these .so libraries on Github, but not the JAR you're using in DependsOn().

@apahl
Copy link
Author

apahl commented Apr 30, 2021

No, I am just tinkering ATM.

But everything required to get the RDKit running on the JVM is available on Github.

The jar and the .so are in this repo:
https://github.com/rdkit/knime-rdkit

The jar is here:
https://github.com/rdkit/knime-rdkit/blob/master/org.rdkit.knime.types/lib/org.RDKit.jar

And the .so is here:
https://github.com/rdkit/knime-rdkit/blob/master/org.rdkit.knime.bin.linux.x86_64/os/linux/x86_64/libGraphMolWrap.so

Thanks for your help.

@ileasile
Copy link
Contributor

ileasile commented Apr 30, 2021

Thanks, reproduced.
It is a weird issue related to classloaders, but I've built up a JAR for you (unpack it from ZIP first). It works without any additional settings, just specify path to it in the @file:DependsOn() and you're all set.
rdkit-jupyter-1.0.0.jar.zip

I have also published this JAR in Maven Central, so you may avoid downloading it manually:
@file:DependsOn("org.jetbrains.kotlinx:rdkit-jupyter:1.0.0")

You may also build this JAR from this repo using ./gradlew shadowJar

@ileasile
Copy link
Contributor

image

@ileasile ileasile added the bug Installation and functionality issues label Apr 30, 2021
@ileasile
Copy link
Contributor

ileasile commented May 1, 2021

Problem is following. To be successfully used in some code, native library should be loaded by the classloader that loaded this code or by one of its parents. In our case Lines classloaders (classloaders that load compiled snippets classes) have classloaders which load JARs as parents, and that's why the code in these JARs can't see the loaded native libraries. This issue is inside REPL evaluator, and reproduces in IDE scripting (i.e. main.kts scripts) as well.

@apahl
Copy link
Author

apahl commented May 1, 2021

Wow, very cool.
I did not understand half of your explanation (only getting started with Kotlin and have no real prior experience with the JVM, but have used a number of other programming languages).

Thanks a TON for your help!!!!

Kind regards,
Axel

@ileasile
Copy link
Contributor

ileasile commented May 2, 2021

Hope it helped!

@ileasile ileasile closed this as completed May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Installation and functionality issues REPL involved
Projects
None yet
Development

No branches or pull requests

2 participants