Problem connecting Java and Prolog with JPL

916 Views Asked by At

I wanted to connect Java and Swi Prolog together using JPL. When I added the library to my project on Intellij the code compiled and when I tried to run a query I got a runtime error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpl in java.library.path: [C:\Program Files\Java\jdk-12\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, c:\swipl\bin, ${env_var:PATH}, .]
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:827)
at java.base/java.lang.System.loadLibrary(System.java:1902)
at org.jpl7.JPL.loadNativeLibrary(JPL.java:114)
at org.jpl7.fli.Prolog.<clinit>(Prolog.java:71)
at org.jpl7.Query.open(Query.java:369)
at org.jpl7.Term.textToTerm(Term.java:155)
at org.jpl7.Query.<init>(Query.java:169)
at Main.main(Main.java:7)

I have the swi prolog 64 bit.

I've tried uninstalling it and use the 32 bit but it did not work.

What I did so far:

I added SWI_HOME_DIR to my Environment Variables. I also added the swi path to Path variable. I added the jpl library to my project (and it added it successfully).

The code I was trying to run:

import org.jpl7.*;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Query q = new Query("true");
        q.hasSolution();

        Map<String,Term>[] res = q.allSolutions();

        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }
    }
}
1

There are 1 best solutions below

7
On

So, is jpl.dll in any of the listed directories:

C:\Program Files\Java\jdk-12\bin   ... probably not 
C:\WINDOWS\Sun\Java\bin            ... probably not
C:\WINDOWS\system32                ... probably not
C:\WINDOWS                         ... probably not
c:\swipl\bin                       ... apparently yes as c:\swipl\bin\jpl.dll exists?
${env_var:PATH}                    ... apparently not

Try the suggestion from this question in your Java program:

File nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);

System.load(nativeFile);

Note that just having jpl.jar is not enough. One needs the jpl.dll file, too. jpl.jar is good for the Java part of the Java-Prolog bridge, but to be able to call a non-JVM compilate, we need to get into system-level details, Hence the dll file.

See troubleshooting tips here: JPL Deploying for users - on Windows

From the above page:

If the Java examples complain that

The dynamic link library libpl.dll could not be found in the specified path

or

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\paul\bin\jpl.dll: Can't find dependent libraries

then there is no SWI-Prolog library libpl.dll in any folder on your PATH: you should have a PATH entry such as C:\Program Files\pl\bin.

The libpl.dll should contain the code for SWI-Prolog itself.