jpl 库不适用于 java maven 项目
jpl library won't work with java maven project
我在项目中使用 jpl 库时遇到问题。
奇怪的是,在一个旧项目中,这是一个普通的 Java 应用程序,通过遵循此解决方案 JPL/SWI Prolog configuration failure 可以很好地与 jpl 库一起工作。该解决方案提到我必须配置我的环境变量才能使库与 Java 对话。
另一方面,我的新项目是maven项目。首先,我包含了 Maven 的以下依赖项。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.4.0</version>
<type>jar</type>
</dependency>
但我遇到了以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError:
org.jpl7.fli.Prolog.current_query()Lorg/jpl7/fli/qid_t; at
org.jpl7.fli.Prolog.current_query(Native Method) at
org.jpl7.Query.close(Query.java:540) at
org.jpl7.Util.textToTerm(Util.java:130) at
org.jpl7.Query.(Query.java:185) at
com.michalistourism.core.PrologHandler.RunGorgias(PrologHandler.java:47)
at
com.persistence.TourismSession.RunPrologFile(TourismSession.java:281)
at
com.michalistourism.tourismassistant.ChatBoxPanel.btnDoNowActionPerformed(ChatBoxPanel.java:109)
at
com.michalistourism.tourismassistant.ChatBoxPanel.access0(ChatBoxPanel.java:22)
at
com.michalistourism.tourismassistant.ChatBoxPanel.actionPerformed(ChatBoxPanel.java:74)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at
java.awt.Component.processEvent(Component.java:6298) at
java.awt.Container.processEvent(Container.java:2236) at
java.awt.Component.dispatchEventImpl(Component.java:4889) at
java.awt.Container.dispatchEventImpl(Container.java:2294) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280) at
java.awt.Window.dispatchEventImpl(Window.java:2746) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at
java.awt.EventQueue.access0(EventQueue.java:97) at
java.awt.EventQueue.run(EventQueue.java:709) at
java.awt.EventQueue.run(EventQueue.java:703) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue.run(EventQueue.java:731) at
java.awt.EventQueue.run(EventQueue.java:729) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这是我抛出上述异常的代码片段。
public PrologReaderCollection RunGorgias(List<Context> contexts) {
JPL.init();
String filename = prologFilePath;
filename = filename.replace('\', '/');
String t1 = "consult('" + filename + "')";
PrologReaderCollection output = new PrologReaderCollection();
Integer count =1;
for(Context cont : contexts) {
Query q1 = new Query(t1);
//Query q1 = new Query("consult",new Term[] {new Atom(t1)});
if (q1.hasNext()) {
String Request = "prove([" + cont.getName().toLowerCase() + "(" + cont.getParameters() + ")],Expl).";
q1 = new Query(Request);
while (q1.hasNext()) {
Map<String, Term> map = q1.nextSolution();
String expl = map.get("Expl").toString();
PrologReader pr = new PrologReader("Solution " + count + " for " + cont.getName().toLowerCase() + ":\n" + expl + "\n\n",cont.getSet(),cont.getName().toLowerCase());
output.add(pr);
count += 1;
if(count > 3) {
break;
}
}
}
count = 1;
}
return output;
}
然后我尝试添加一个指向 jpl jar 的 maven 依赖项,我在之前的 Java 项目中使用过,其中 jar 文件位于项目 lib 文件夹中。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.4.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jpl.jar</systemPath>
</dependency>
本例中的新异常如下:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError:
org/jpl7/JPL at
com.michalistourism.core.PrologHandler.RunGorgias(PrologHandler.java:37)
at
com.persistence.TourismSession.RunPrologFile(TourismSession.java:281)
at
com.michalistourism.tourismassistant.ChatBoxPanel.btnDoNowActionPerformed(ChatBoxPanel.java:109)
at
com.michalistourism.tourismassistant.ChatBoxPanel.access0(ChatBoxPanel.java:22)
at
com.michalistourism.tourismassistant.ChatBoxPanel.actionPerformed(ChatBoxPanel.java:74)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at
java.awt.Component.processEvent(Component.java:6298) at
java.awt.Container.processEvent(Container.java:2236) at
java.awt.Component.dispatchEventImpl(Component.java:4889) at
java.awt.Container.dispatchEventImpl(Container.java:2294) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280) at
java.awt.Window.dispatchEventImpl(Window.java:2746) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at
java.awt.EventQueue.access0(EventQueue.java:97) at
java.awt.EventQueue.run(EventQueue.java:709) at
java.awt.EventQueue.run(EventQueue.java:703) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue.run(EventQueue.java:731) at
java.awt.EventQueue.run(EventQueue.java:729) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: org.jpl7.JPL at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 41 more
有什么我可以做的吗?
我通过引用 jpl 的 7.0.1 版本而不是 7.4.0 版本的 maven 找到了解决我的问题的方法,并且成功了!显然我需要一个旧版本的库。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.0.1</version>
</dependency>
我在项目中使用 jpl 库时遇到问题。
奇怪的是,在一个旧项目中,这是一个普通的 Java 应用程序,通过遵循此解决方案 JPL/SWI Prolog configuration failure 可以很好地与 jpl 库一起工作。该解决方案提到我必须配置我的环境变量才能使库与 Java 对话。
另一方面,我的新项目是maven项目。首先,我包含了 Maven 的以下依赖项。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.4.0</version>
<type>jar</type>
</dependency>
但我遇到了以下异常:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: org.jpl7.fli.Prolog.current_query()Lorg/jpl7/fli/qid_t; at org.jpl7.fli.Prolog.current_query(Native Method) at org.jpl7.Query.close(Query.java:540) at org.jpl7.Util.textToTerm(Util.java:130) at org.jpl7.Query.(Query.java:185) at com.michalistourism.core.PrologHandler.RunGorgias(PrologHandler.java:47) at com.persistence.TourismSession.RunPrologFile(TourismSession.java:281) at com.michalistourism.tourismassistant.ChatBoxPanel.btnDoNowActionPerformed(ChatBoxPanel.java:109) at com.michalistourism.tourismassistant.ChatBoxPanel.access0(ChatBoxPanel.java:22) at com.michalistourism.tourismassistant.ChatBoxPanel.actionPerformed(ChatBoxPanel.java:74) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access0(EventQueue.java:97) at java.awt.EventQueue.run(EventQueue.java:709) at java.awt.EventQueue.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue.run(EventQueue.java:731) at java.awt.EventQueue.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这是我抛出上述异常的代码片段。
public PrologReaderCollection RunGorgias(List<Context> contexts) {
JPL.init();
String filename = prologFilePath;
filename = filename.replace('\', '/');
String t1 = "consult('" + filename + "')";
PrologReaderCollection output = new PrologReaderCollection();
Integer count =1;
for(Context cont : contexts) {
Query q1 = new Query(t1);
//Query q1 = new Query("consult",new Term[] {new Atom(t1)});
if (q1.hasNext()) {
String Request = "prove([" + cont.getName().toLowerCase() + "(" + cont.getParameters() + ")],Expl).";
q1 = new Query(Request);
while (q1.hasNext()) {
Map<String, Term> map = q1.nextSolution();
String expl = map.get("Expl").toString();
PrologReader pr = new PrologReader("Solution " + count + " for " + cont.getName().toLowerCase() + ":\n" + expl + "\n\n",cont.getSet(),cont.getName().toLowerCase());
output.add(pr);
count += 1;
if(count > 3) {
break;
}
}
}
count = 1;
}
return output;
}
然后我尝试添加一个指向 jpl jar 的 maven 依赖项,我在之前的 Java 项目中使用过,其中 jar 文件位于项目 lib 文件夹中。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.4.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jpl.jar</systemPath>
</dependency>
本例中的新异常如下:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/jpl7/JPL at com.michalistourism.core.PrologHandler.RunGorgias(PrologHandler.java:37) at com.persistence.TourismSession.RunPrologFile(TourismSession.java:281) at com.michalistourism.tourismassistant.ChatBoxPanel.btnDoNowActionPerformed(ChatBoxPanel.java:109) at com.michalistourism.tourismassistant.ChatBoxPanel.access0(ChatBoxPanel.java:22) at com.michalistourism.tourismassistant.ChatBoxPanel.actionPerformed(ChatBoxPanel.java:74) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access0(EventQueue.java:97) at java.awt.EventQueue.run(EventQueue.java:709) at java.awt.EventQueue.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue.run(EventQueue.java:731) at java.awt.EventQueue.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Caused by: java.lang.ClassNotFoundException: org.jpl7.JPL at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 41 more
有什么我可以做的吗?
我通过引用 jpl 的 7.0.1 版本而不是 7.4.0 版本的 maven 找到了解决我的问题的方法,并且成功了!显然我需要一个旧版本的库。
<dependency>
<groupId>jpl</groupId>
<artifactId>jpl</artifactId>
<version>7.0.1</version>
</dependency>