PackageDescription: JNIPort(Bundle)
JNI PortLast published: September 27, 2009 by 'JoachimGeidel'
This bundle contains an interface to the Java Virtual Machine. It is based in the Java Native Interface (JNI). The original implementation was done by Chris Uppal in Dolphin Smalltalk. The bundle contains a port of this implementation to VisualWorks. Documentation is online at
http://jniport.wikispaces.com/
The documentation is work in progress. You can monitor updates by subscribing to the RSS feed at
http://jniport.wikispaces.com/space/xmla?v=rss_2_0
To use JNIPort, you also need the following components:
- "JNIPort VW75 Extensions" (only in in VisualWorks 7.5 or earlier),
- "JNIPort VW76 Extensions" (only in in VisualWorks 7.6),
- "JNIPort Prerequisites",
- "Registry" (version 23 or later),
- "FastCMethodPointers" (version 1.1 or later),
- "WeakCollections" (version 2 or later).
Tests for JNIPort are available in "JNIPort Tests", and development tools in "JNIPort Tools".
These components are available as parcels in the "Contributed" directory of the VisualWorks distribution, starting with VisualWorks 7.7. Alternatively, you can load them from the Cincom Public Repository.
If you load JNIPort from a Store repository, you should configure the Store > Prerequisites settings in the VisualWorks Settings tool such that Store always loads the latest versions with blessing level "Development", and to search bundles and packages first. Some of the prerequisites are distributed with VisualWorks as parcels, e.g. the Registry parcel in the Contributed directory of the VisualWorks distribution. If you want to load these parcels instead of packages from a Store repository, set the search policy for prerequisites of type #any to "Search parcels first" in the Store > Prerequisites settings.
The bundle JNIPort contains two archives with additional files:
JNIPort_Extras.zip This archive contains the following files:
DolphinJNIHelper.dll and its source code
This library is needed for handling the hook functions provided by JNI for monitoring the state of a running JVM. See the package comment of ''CU JNI Helper''.
Currently only available for MS Windows.
JNIPort.jar and its source code in JNIPort.zip
This Java library is necessary for handling callbacks from Java to Smalltalk.
JNIPort-Tests.jar and its source code in JNIPort-Tests.zip
This Java library contains classes which are needed for running the tests in the bundle ''JNIPort Tests''.
JNIPort_Docs.zip This archive contains the original JNIPort documentation written by Chris Uppal. Please read this before using JNIPort.
Both files are taken from the Dolphin Smalltalk version of JNIPort.
Example for using JNIPort with automatic generation of wrapper classes and methods for Java classes (when evaluating the code, ignore warnings about undefined selectors):
------------------------------------------------[
| jvmSettings jvm zfClass zipfile entries |
jvmSettings := (JNIPort.JVMSettings new)
name: 'JVM with ghost class generation';
yourself.
jvmSettings usesGhosts: true.
jvmSettings jniPortSettings useJNIHelperLibrary: false.
jvm := JNIPort.JVM newWithSettings: jvmSettings.
zfClass := jvm findClass: #'java.util.zip.ZipFile'.
zipfile := zfClass new_String: 'MyZipFile.zip'.
zipfile size_null. "--> answers an Integer"
entries := zipfile entries_null.
entries asAnEnumeration do: [:each | Transcript cr; print: each].
]------------------------------------------------
Querying the JNI version:
------------------------------------------------[
| vmargs env status version |
vmargs := JNIPort.JavaVMInitArgs new.
vmargs
addOption: '-verbose:jni';
ignoreUnrecognized: true.
[env := JNIPort.JNILibrary new createFirstJNIEnv: vmargs]
on: JNIPort.JNIError
do: [:error | ^Dialog warn: ('JVM startup failed with: ', (JNIPort.JNILibrary lookupErrorCode: error parameter))].
version := env GetVersion.
Transcript show: 'JNI version: '; print: (version printStringRadix: 16); cr; endEntry.
"After executing the following expressions, you will have to restart the image to be able to work with a Java VM again."
status := env javaVM DestroyJavaVM.
status ~= JNIPort.JNIInterface current JNI_OK
ifTrue: [Dialog warn: 'JVM shutdown failed with: ', (JNILibrary lookupErrorCode: error parameter)].
]------------------------------------------------