Tag Archives: Java connection ffi

JNIPort for Pharo 3.0

Hi everybody,

JNIPort for Pharo 3.0 alpha is now available at SmalltalkHub.

JNIPort is a Smalltalk library which allows Java code to be invoked from
Smalltalk. It acts as a bridge between the world of Smalltalk objects and a
Java Virtual Machine (JVM) where Java code is executing.

When I first ported JNIPort to Squeak and Pharo 1.x, I used Alien as the FFI
library. As NativeBoost has superseded Alien, the interface to the Java
Native Interface library had to be rewritten. There are still some
undocumented and obscure parts in NativeBoost, so this has taken some time.

If you want to try it, load it via the ConfigurationOfJNIPort. I hope this
works. If it does not, load the packages individually in the order in which
they appear in the ConfigurationOfJNIPort.
The next step is to read the documentation at
Otherwise, you won’t know what to do next. 😉
Download the extra files from the download page at Wikispaces, you will need
two jar files from there.

Start a Java VM attached to Pharo by adapting and executing the following
code snippet:

| jvmSettings |
“You can set the path to the Java VM library by editing the path as
JNIPortJNIInterface libraryFile:
“JNIPortJNIInterface libraryFile:
‘C:\Programme\Java\jre7\bin\client\jvm.dll’ .”

jvmSettings := JVMSettings new.
jvmSettings usesGhosts: true.
jvmSettings supportsCallbacks: true.
jvmSettings jniPortSettings useJNIHelperLibrary: false.
jvmSettings ghostClassSettings retainMethodSource: true.

“On Mac OS X and Linux, class path entries are separated by colons.”
“On Windows, you have to use semicolons instead. Edit the following line as
jvmSettings runtimeSettings classpath:
” jvmSettings runtimeSettings classpath:

“Uncomment the following if you want the Java VM to emit verbose messages.
How to access these messages depends on your platform.
On Mac OS X, they are visible in the Console application.”
“jvmSettings runtimeSettings addOption: ‘-verbose:jni’; addOption:
‘-verbose:gc’; addOption: ‘-verbose:class’.”
Cursor execute showWhile: [JVM newWithSettings: jvmSettings].

Then the fun starts:

| zfClass zipfile entries |
zfClass := JVM current findClass: #’java.util.zip.ZipFile’.
zipfile := zfClass new_String: ‘JNIPort.jar’.
zipfile size. “–> answers an Integer”
entries := zipfile entries.
entries asAnEnumeration do: [:each | Transcript cr; print: each].

And now to the ugly little secret reason why I call it an alpha version:

While everything seems to work, it is *much* slower than it used to be in
Pharo 1.2 with Alien. And when I compare it with VisualWorks, I wonder if
the Pharo version secretly does some additional work for SETI:
– Starting the JVM:
VisualWorks 0.7 seconds
Pharo 7 to 8 seconds
– A benchmark using some methods of java.io.BufferedReader and
VisualWorks 114 milliseconds
Pharo 12 to 13 seconds

I have not yet found the reason. The TimeProfiler is not very helpful, as
its 1 millisecond resolution is too coarse. It would be nice if it had
microsecond resolution. From the TimeProfiler results, it seems that calling
a JNI method with NativeBoost is extremely slow, but I don’t trust those
profiles too much.

So, give it a try, and if you find something useful concerning the bad
performance, please send me a message.

Best regards,
Joachim Geidel