问题是在使用JPL连接Java和Prolog时遇到了困扰。

huangapple go评论70阅读模式
英文:

Problem connecting Java and Prolog with JPL

问题

我想要使用JPL将Java和Swi Prolog连接起来。
当我将该库添加到我的Intellij项目中时,代码编译通过,但当我尝试运行查询时,出现了运行时错误:

    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)

我使用了64位的swi prolog。

我尝试过卸载并使用32位的版本,但没有成功。

到目前为止我做了以下操作:

我将SWI_HOME_DIR添加到了环境变量中。
我还将swi路径添加到了Path变量中。
我将jpl库添加到了我的项目中(添加成功)。

我尝试运行的代码:

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]);
        }
    }
}
英文:

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 &quot;main&quot; 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.&lt;clinit&gt;(Prolog.java:71)
at org.jpl7.Query.open(Query.java:369)
at org.jpl7.Term.textToTerm(Term.java:155)
at org.jpl7.Query.&lt;init&gt;(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(&quot;true&quot;);
        q.hasSolution();

        Map&lt;String,Term&gt;[] res = q.allSolutions();

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

答案1

得分: 1

所以,在列出的目录中是否存在 jpl.dll 文件:

C:\Program Files\Java\jdk-12\bin   ... 可能不存在
C:\WINDOWS\Sun\Java\bin            ... 可能不存在
C:\WINDOWS\system32                ... 可能不存在
C:\WINDOWS                         ... 可能不存在
c:\swipl\bin                       ... 显然存在,因为 c:\swipl\bin\jpl.dll 存在?
${env_var:PATH}                    ... 显然不存在

尝试在您的Java程序中使用此问题中的建议:

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

System.load(nativeFile);

请注意,仅有 jpl.jar 是不够的。我们还需要 jpl.dll 文件。jpl.jar 对于Java部分是有用的,但是为了能够调用非JVM编译的内容,我们需要涉及系统级细节,因此需要dll文件。

在这里查看故障排除提示:JPL用户部署 - 在Windows上

从上述页面中:

> 如果Java示例出现以下问题:
>
> 找不到指定路径中的动态链接库libpl.dll
>
> 或者
>
> Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\paul\bin\jpl.dll: 无法找到依赖库
>
> 那么在您的PATH中没有SWI-Prolog库libpl.dll,您应该有一个类似C:\Program Files\pl\binPATH条目。

libpl.dll 应该包含SWI-Prolog本身的代码。

英文:

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 + &quot;.dll&quot;);
    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 &quot;main&quot; java.lang.UnsatisfiedLinkError: C:\paul\bin\jpl.dll: Can&#39;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
&gt; Files\pl\bin
.

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

huangapple
  • 本文由 发表于 2020年9月12日 02:05:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63852186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定