javamavenjpl

jpl library won't work with java maven project


I have an issue using jpl library within my project.

The weird thing is that on an old project, that was an ordinary Java application, worked fine with jpl library by following this solution JPL/SWI Prolog configuration failure. The solution mentioned that I had to configure my enviromental variables in order to make the library talk with Java.

On the other hand, my new project is a maven project. First I included the below dependecy of Maven.

    <dependency>
        <groupId>jpl</groupId>
        <artifactId>jpl</artifactId>
        <version>7.4.0</version>
        <type>jar</type>
    </dependency>

But I was getting the below exception:

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.access$100(ChatBoxPanel.java:22) at com.michalistourism.tourismassistant.ChatBoxPanel$2.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.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.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$4.run(EventQueue.java:731) at java.awt.EventQueue$4.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)

This is a snippet of my code where the above exception is thrown.

    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;
}

Then I tried to add a maven dependecy that points to a jpl jar, that I used on my previous Java project, where the jar file is inside projects lib folder.

    <dependency>
        <groupId>jpl</groupId>
        <artifactId>jpl</artifactId>
        <version>7.4.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/jpl.jar</systemPath>
    </dependency> 

project's file structure

The new exception in this case is the below one:

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.access$100(ChatBoxPanel.java:22) at com.michalistourism.tourismassistant.ChatBoxPanel$2.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.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.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$4.run(EventQueue.java:731) at java.awt.EventQueue$4.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

Is there something I can do?


Solution

  • I've managed to find a solution to my problem by referencing with maven the 7.0.1 version of jpl rather than 7.4.0 and it worked! Apparently I needed an older version of the library.

    <dependency>
         <groupId>jpl</groupId>
         <artifactId>jpl</artifactId>
         <version>7.0.1</version>  
    </dependency>