For my own documentation I went ahead and summarized all the steps I had to take
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.
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)]
After running the
setDefaultInitArgs(String args) you can test if blip compiles and start by running
JPL.init(); from the Java code.
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 set
LD_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-184.108.40.206/jre/lib/i386/client/:/usr/lib/jvm/java-1.5.0-sun-220.127.116.11/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
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:
[plugin-name].productfile you can add this to the
<vmArgs>tag in that file.
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-18.104.22.168/jre/lib/i386/client/:/usr/lib/jvm/java-1.5.0-sun-22.214.171.124/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).