Accessing BLIPKIT from command line Java or Eclipse Plug-in in Ubuntu

For my own documentation I went ahead and summarized all the steps I had to take

  1. In order to start blipkit from java commandline, and
  2. In order to start blipkit from an Eclipse plugin.

Introduction

This tutorial applies for Ubuntu Linux 9.04 and assumes you have installed SWI-Prolog 5.7.x. including all it's packages + BLIPKIT, and verified that the JPL Java examples work.

Accessing prolog from Java (commandline Java + Eclipse plug-in)

In order to access SWI-Prolog from Java you you need to somehow give your plug-in access to the JPL Java source code.

For running Java from the commandline, you should just need include the Java classes in [swi-prolog-src]/packages/jpl/src/java/jpl into your program.

For an Eclipse plugin, the simplest way, which is verified to work, is to just drag and drop the folder [swi-prolog-src]/packages/jpl/src/java/jpl into your Eclipse Plug-in:s src folder.

Then study the Java code of [swi-prolog-src]/packages/jpl/examples/java/Test/Test.java to see how to import the jpl classes and run prolog queries.

Mimicking the Blipkit startup script (commandline Java + Eclipse plug-in)

The BLIPKIT startup script preloads SWI-Prolog with a bunch of prolog modules, including the blip core and extra blip modules. It also passes (on the first line) certain flags to prolog in order to give it enough memory etc.

This specialized prolog initialization can be mimicked with the JPL, by using it's setDefaultInitArgs(String[] args) method.

Before prolog is initialized, send to this method for example the following array of arguments (here one line per array element):

 -L0
 -G0
 -A0
 -T0
 -q
 -g 
 main
 -t
 halt
 -s
 [path to file with prolog code for loading blip modules (blip startup script except first line)]

Create the file at the path you give as the last argument, and copy into it the whole content of the blip startup script except the first line (that starts with a '#').

After running the setDefaultInitArgs(String[] args) you can test if blip compiles and start by running JPL.init(); from the Java code.

Steps specific for command line Java

You have to execute java with a shell script that precedes the java call with LD_PRELOAD=[path to libjpl.so] so that it gets loaded before the Java VM.* Before that you also need to setLD_LIBRARY_PATH to the folders where libjava.so and libjvm.so of your Java VM are located.

Example script (Ubuntu 9.04, Java 1.5):

 # Set LD_LIBRARY_PATH environment variable
 
 export    LD_LIBRARY_PATH="/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/jre/lib/i386/client/:/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/jre/lib/i386/"
 
 # Preload libjpl.so and execute Java class
 
 LD_PRELOAD=/usr/local/lib/pl-5.7.15/lib/i686-linux/libjpl.so    java YourJavaClass

That's it.

Steps specific for Eclipse Plug-ins

Give the path to libjpl.so to Java

Make sure to set java.library.path to contain the path to the folder containing libjpl.so. Do this in the run configuration for your plug-in, in the vm-arguments. It is set with the -D flag, like so:

 -Djava.library.path=/usr/local/lib/pl-5.7.15/lib/i686-linux

If you have an Eclipse [plugin-name].product file you can add this to the <vmArgs> tag in that file.

Make sure libjpl.so is loaded before the Java VM

Startup Eclipse with a shell script that precedes the eclipse call with LD_PRELOAD=[path to libjpl.so] so that it gets loaded before Eclipse (and thereby the Java VM).*

Before that you also need to set LD_LIBRARY_PATH to the folders where libjava.so and libjvm.so of your Java VM are located, since LD_PRELOAD seems to empty it.

An example script for an Ubuntu 9.04 box with Java 1.5 (non-standard location of eclipse):

 # Set LD_LIBRARY_PATH environment variable
 
 export    LD_LIBRARY_PATH="/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/jre/lib/i386/client/:/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/jre/lib/i386/"
 
 # Preload libjpl.so and start Eclipse
 
 LD_PRELOAD=/usr/local/lib/pl-5.7.15/lib/i686-linux/libjpl.so    ~/bin/eclipse/eclipse &

If you can see a long list of prolog modules compiled in the Console, but no errors, You're happy.


* This seems to be due to a problem described in the ISSUES file attached with jpl (swi-prolog/jpl/ISSUES). According to that file, if libjpl.so is loaded directly from Java (which it is if you use only LD_LIBRARY_PATH and not LD_PRELOAD) it will use the prolog kernel included in libjpl.so, which doesn't seem to be as complete as the "normal" prolog engine. (for example, it complains about a missing symbol "PL_new_atom" (which can actually found in the prolog source code, in src/pl-fli.c), when compiling some blipkit modules).