by Arnaud Trouche - (no comment)
Objectives
As introduced in my previous article, we have decided to use the OSGi framework at Artenum as the basis of our modular projects.I have already written about how to embed VTK, the C++ open source Visualization Toolkit, in an OSGi bundle for Mac OS X. The purpose of this new article is to detail how to do it on Linux. As I already detailed how JNI and OSGi work with native libraries in my previous article, I will not go through these points again.This article is divided in two parts:- 1) Compiling VTK, a detailed explanation of how to compile VTK on Linux
- a) Hardware and operating system
- b) Prerequisites
- c) Compiling VTK with Java wrapping
- 2) Embedding VTK in an OSGi bundle, describing Linux specific constraints encountered.
- a) Prerequisites
- b) Embedding VTK in an OSGi bundle
1) Compiling VTK
a) Hardware and operating system
For this article, I have used a clean Ubuntu 10.10 64 bits distribution installed on my MacBook Pro 3.1 with 2.2GHz Intel Core 2 Duo processors. At installation, I have selected the option to automatically download the updates during installation. When I first started opened a session, I was thus proposed to install all updates, including Embedded GNU C Library (libc6 version 2.12.1) that is required to compile VTK.b) Prerequisites
Here are the prerequisites to compile the VTK library:- Sun JDK 6: to install Sun JDK, use the command lines below.
> sudo add-apt-repository "deb http://archive.canonical.com/ubuntu maverick partner"
> sudo apt-get update
> sudo apt-get install sun-java6-jdk
> java -version
- g++ to compile C++ code:
> sudo apt-get install g++
- OpenGL:
> sudo apt-get install libgl1-mesa-dev
- X11 toolkit intrinsics library (development headers):
> sudo apt-get install libxt-dev
- VTK latest source code (5.6.1 at the time this article was written).
- CMake, the cross-platform make, used to generate makefiles to compile VTK. To install it, use the command lines below.
> sudo apt-get install cmake-curses-gui
c) Compiling VTK with Java wrapping
We are now ready to compile VTK.First, create a vtk-5.6.1 directory in your home folder. This is where VTK will be installed.Then go to the ~/vtk-5.6.1-src/build directory and type:> ccmake -i ..
BUILD_EXAMPLES OFF BUILD_SHARED_LIBS ON BUILD_TESTING OFF CMAKE_BACKWARDS_COMPATIBILITY 2.4 CMAKE_BUILD_TYPE Release __CMAKE_INSTALL_PREFIX /home/atrouche/vtk-5.8.0__ VTK_DATA_ROOT VTK_DATA_ROOT-NOTFOUND VTK_EXTRA_COMPILER_WARNINGS OFF VTK_LARGE_DATA_ROOT VTK_LARGE_DATA_ROOT-NOTFOUND VTK_USE_CHARTS ON VTK_USE_GEOVIS ON VTK_USE_INFOVIS ON VTK_USE_N_WAY_ARRAYS ON VTK_USE_PARALLEL ON VTK_USE_QT OFF VTK_USE_RENDERING ON VTK_USE_TEXT_ANALYSIS OFF VTK_USE_VIEWS ON VTK_WRAP_JAVA ON VTK_WRAP_PYTHON OFF VTK_WRAP_PYTHON_SIP OFF VTK_WRAP_TCL OFF
- depending on the machine and what you have already installed on, you may have to indicate the correct compiler by modifying the two following lines (with your path to g++/gcc):
CMAKE_CXX_COMPILER /usr/bin/g++ CMAKE_C_COMPILER /usr/bin/gcc
- if you have an error when typing c such as "JAVA_AWT_INCLUDE_PATH NOT FOUND", you have to define the java following flags (here is an exemple I used with Java 8, path may vary depending on the java version you have, check every line). Also, it is a good way to check that cmake is using the correct java version (seeing the path it found):
JAVA_AWT_INCLUDE_PATH /usr/lib64/jvm/java/include JAVA_AWT_LIBRARY /usr/lib64/jvm/java/jre/lib/amd64/libjawt.so JAVA_INCLUDE_PATH /usr/lib64/jvm/java/include JAVA_INCLUDE_PATH2 /usr/lib64/jvm/java/include/linux JAVA_JVM_LIBRARY /usr/lib64/jvm/java/jre/lib/amd64/server/libjvm.so Java_IDLJ_EXECUTABLE /usr/lib64/jvm/java/bin/idlj Java_JARSIGNER_EXECUTABLE /usr/lib64/jvm/java/bin/jarsigner Java_JAR_EXECUTABLE /usr/lib64/jvm/java/bin/jar Java_JAVAC_EXECUTABLE /usr/lib64/jvm/java/bin/javac Java_JAVADOC_EXECUTABLE /usr/lib64/jvm/java/bin/javadoc Java_JAVAH_EXECUTABLE /usr/lib64/jvm/java/bin/javah Java_JAVA_EXECUTABLE /usr/lib64/jvm/java/bin/java
> make -j 2
> make install
2) Embedding VTK in an OSGi bundle
a) Prerequisites
- The sample project provided here are built with Maven. To install it, use the command line:
> sudo apt-get install maven2
- You will also need an Integrated Development Environment, such as Eclipse, which can be downloaded here: http://www.eclipse.org/downloads.
- Finally, to run the OSGi Framework and execute the VTK bundle, you will need an implementation of the framework. Two popular open-source implementations are Equinox and Felix.
b) Embedding VTK in an OSGi bundle
The OSGi bundle embedding VTK for Linux is very similar to the one in my previous article. You can download the project (with the compiled VTK libraries) here: test-osgi-vtk-linux64b.zip.In this project, we have created two classes:- org.test.vtk.osgi.TestVTK is the same JFrame that contains the method start() to initialize a vtkPanel containing the 3D cone. It also contains the stop()method called when the bundle is stopped.
- org.test.vtk.osgi.Activator is the BundleActivator class that will be called by OSGi when the bundle is started or stopped. It statically loads the VTK libraries one by one, in the correct order of their dependencies, as shown below:
static { System.loadLibrary("vtkproj4"); System.loadLibrary("vtkalglib"); System.loadLibrary("vtksys"); System.loadLibrary("vtkCommon"); System.loadLibrary("vtkFiltering"); System.loadLibrary("vtkexpat"); System.loadLibrary("vtkjpeg"); System.loadLibrary("vtkzlib"); System.loadLibrary("vtklibxml2"); System.loadLibrary("vtktiff"); System.loadLibrary("vtkpng"); System.loadLibrary("vtksqlite"); System.loadLibrary("vtkmetaio"); System.loadLibrary("vtkNetCDF"); System.loadLibrary("vtkDICOMParser"); System.loadLibrary("vtkNetCDF_cxx"); System.loadLibrary("vtkIO"); System.loadLibrary("vtkImaging"); System.loadLibrary("vtkverdict"); System.loadLibrary("vtkGraphics"); System.loadLibrary("vtkfreetype"); System.loadLibrary("vtkftgl"); System.loadLibrary("vtkGenericFiltering"); System.loadLibrary("vtkRendering"); System.loadLibrary("vtkexoIIc"); System.loadLibrary("Cosmo"); System.loadLibrary("VPIC"); System.loadLibrary("vtkParallel"); System.loadLibrary("vtkHybrid"); System.loadLibrary("vtkWidgets"); System.loadLibrary("vtkVolumeRendering"); }
# First we delete the cache folder contraining previously loaded bundles rm -rf configuration/org.eclipse.osgi configuration *.log # Then we execute Equinox with the argument -Xcheck:jni to see JNI-related errors java -Xcheck:jni -jar org.eclipse.osgi_3.6.1.R36x_v20100806.jar -console osgi> install file:/home/ben/workspace/test-osgi-vtk-linux64b/target/test-osgi-vtk-linux64b-1.0-SNAPSHOT.jar osgi> start 1
export LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun-1.6.0.21/jre/lib/i386/xawt