1

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]);
        }
    }
}
Danny
  • 125
  • 1
  • 7
  • 1
    According to the [**documentation**](https://jpl7.org/Deployment): *"In Windows, `jpl.dll` can go in any folder on your `PATH`; perhaps `%SWI_HOME_DIR%\bin` or your Windows’ system folder."* --- Is the **`jpl.dll`** file there, in a folder listed on your PATH? Is it the 64-bit version? I'm not asking if the `bin` folder is on your path, I'm asking if that specific file can be found on the path. – Andreas Sep 11 '20 at 18:14
  • Yes, and it is the 64 bit version. – Danny Sep 11 '20 at 18:16
  • 1
    So if you do a `dir c:\swipl\bin\jpl.dll` from a command prompt, it will list the file? And you are running 64-bit Java? And you are *sure* the file is the 64-bit version? If yes to all, then the file might be corrupt, to try getting another copy. – Andreas Sep 11 '20 at 18:23
  • I have jpl.jar not .dll – Danny Sep 11 '20 at 18:33
  • Then the error message is exactly right, huh? Go figure. Next time, read the comment, because my first comment explicitly said **`jpl.dll`**. I even **bolded** it so you could see, but I guess you didn't. – Andreas Sep 11 '20 at 19:53

1 Answers1

1

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.

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
  • I have jpl.jar not .dll – Danny Sep 11 '20 at 18:33
  • 1
    @Danny That's not enough. The JAR file is good for the Java part of the brige, but we need to get closer to the system to load the Prolog Processor and call it. Hence the dll file. – David Tonhofer Sep 11 '20 at 18:35
  • OK I FOUND IT, I put it in the folder, now I have a different exception: Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\swipl\bin\jpl.dll: Can't find dependent libraries at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method) at java.base/java.lang.ClassLoader$NativeLibrary.load(Class... – Danny Sep 11 '20 at 18:37
  • 1
    @Danny See extended answer. – David Tonhofer Sep 11 '20 at 18:41
  • I don't know man I can't get it to work. :( Thank you for you kind help – Danny Sep 11 '20 at 18:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221347/discussion-between-david-tonhofer-and-danny). – David Tonhofer Sep 11 '20 at 19:01
  • @Danny "dependent libraries" means other `.dll` files that the `jpl.dll` depends on. Were there others where you found the `jpl.dll` file? Copy them all, or better yet, add the folder that has them to the PATH in effect when running the program. – Andreas Sep 11 '20 at 19:55