JavaCPP Examples

I finally found the time to write post about the javacpp <a href="https://github.com/Jotschi/javacpp-examples">examples</a> i created since i wanted to explore the capabilities of the <a href="http://code.google.com/p/javacpp/">JavaCPP library</a>.

Please note that the examples are only tested under linux and probably won’t run on windows/mac.

The examples can be fetched from my github repository

So far i created four different examples. Each for a specific usecase:

javacpp-sharedlib-example

This example shows how to use a own cpp shared library with the library.

The example consists of multiple pieces that finally work together.

1. Cube.cpp

The shared lib will be created from the Cube cpp class.

#include "Cube.h"

void Cube::setSide(double s) {
	side = s <= 0 ? 1 : s;
}

double Cube::getSide() {
	return side;
}

double Cube::getArea() {
	return 6 * side * side;
}

double Cube::getVolume() {
	return side * side * side;
}

Cube.h

#ifndef CUBE_H
#define CUBE_H

class Cube {
	private:
		double side;
	public:
		Cube() {};
		~Cube() {};

		// copy constructor
		Cube(class Cube& cube) {
			side = cube.side;
		}

		void setSide(double s);
		double getSide();
		double getArea();
		double getVolume();
};

#endif

2. JavaCube.java

The JavaCube java class acts as a wrapper/glue that fits between the gap of java and native code. Normally you would here defined your jni hooks which then would be implemented. Since i use JavaCPP this step is radically simplified. The annotations are used to give javacpp all the information it needs to prepare its g++ command that in the final compile step will create the library that can be shipped with the java program.

package de.jotschi.javacpp.example;
import static com.googlecode.javacpp.Loader.load;

import com.googlecode.javacpp.Pointer;
import com.googlecode.javacpp.annotation.Name;
import com.googlecode.javacpp.annotation.Platform;

@Platform(include = "Cube.h", link = "Cube")
public class JavaCube {

	NativeCube cube;
	static {
		load();
	}

	public JavaCube() {
		cube = new NativeCube();
	}

	public void setSide(double s) {
		cube.setSide(s);

	}

	public double getArea() {
		return cube.getArea();
	}

	public double getSide() {
		return cube.getSide();
	}

	public double getVolume() {
		return cube.getVolume();
	}

	@Name("Cube")
	public static class NativeCube extends Pointer {

		static {
			load();
		}

		public NativeCube() {
			allocate();
		}

		public NativeCube(Pointer p) {
			super(p);
		}

		public native void setSide(double s);

		// this = new Cube()
		private native void allocate();

		private native double getArea();

		private native double getSide();

		private native double getVolume();

	}
}

Once the java files have been compiled the exec-maven-plugin will be executed which itself executes the makefile that builds the shared library.

<execution>
	<id>build-sharedlib</id>
	<phase>process-classes</phase>
	<goals>
		<goal>exec</goal>
	</goals>
	<configuration>
		<executable>make</executable>
		<commandlineArgs>main</commandlineArgs>
		<workingDirectory>${basedir}/src/main/jni</workingDirectory>
	</configuration>
</execution>

This library will be used within the next execution of the exec-maven-plugin. This final execution step will invoke the javacpp builder which generated the jni headers from the java class annotations and it also compiles the jni header interface against the previously created library.

<execution>
	<id>build-jnilib</id>
	<phase>process-classes</phase>
	<goals>
		<goal>exec</goal>
	</goals>
	<configuration>
		<executable>java</executable>
		<commandlineArgs>-jar ../libs/javacpp.jar -Dcompiler.linkpath=${basedir}/target/classes/linux-x86 -Dcompiler.includepath=${basedir}/src/main/jni -classpath target/classes de.jotschi.javacpp.example.JavaCube</commandlineArgs>
	</configuration>
</execution>

The CubeTest.java Junit tests shows how the created java class that wraps the native class works.

javacpp-libc6-example / javacpp-stdlib-example

These examples are very similar to the javacpp-sharedlib-example. The libc6 example show how to use JavaCPP with the libc6 library and c code. The stdlib example on the other hand uses the c++ stdlib.

javacpp-android-example

This example shows how to use a android maven project in combination with JavaCPP. The example shows how to mix java with native code. The java part creates a opengl surface and the native code is used to execute a opengl function that changed the color of the display.