Building GDAL/OGR Java Bindings on Linux/Unix/MinGW

Tested with GDAL 1.5.2, 1.7.0 and 1.9.2.

Configure and Build GDAL

You need swig in the path.

I found I had to configure GDAL --without-libtool to get the java bindings to build.

Note : with GDAL 1.7.0, this restriction is no longer necessary. libtool building should work just fine.

Update java.opt

Replace/Create gdal/swig/java/java.opt with an appropriate local version. Make sure the windows backslashes are turned to forward slashes, and point to your local Java SDK. ant is assumed to be in the path.

JAVA_HOME = /home/warmerda/pkg/jdk1.6.0_10
JAVA_INCLUDE=-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux

OS X Leopard

See this ticket for a patch and java.opt for building the java bindings on Leopard

Win32 MinGW

Tested with GDAL 1.7.0. Building the Java bindings under MinGW is possible but requires a bit of patching :

  • Update the JAVA_HOME variable in gdal/swig/java/java.opt and change the JAVA_INCLUDE variable to the following value :
    JAVA_INCLUDE=-I$(JAVA_HOME)/include -I.

  • in swig/java/GNUmakefile, we ensure that the name of the DLLs are not prefixed by lib, by changing the 'JAVA_MODULES= libgdaljni.$(SO_EXT)...' and '$(JAVA_MODULES): %jni.$(SO_EXT)...' lines to be
JAVA_MODULES = gdaljni.$(SO_EXT) ogrjni.$(SO_EXT) gdalconstjni.$(SO_EXT) osrjni.$(SO_EXT)


$(JAVA_MODULES): %jni.$(SO_EXT): %_wrap.$(OBJ_EXT)
  • Create a jni_md.h file in gdal/swig/java directory with the following content that ensures thanks to the empty JNICALL that the symbols in the *jni.dll have the proper decorations
#ifndef _GDAL_JNI_MD_H_
#define _GDAL_JNI_MD_H_

#define JNIEXPORT __declspec(dllexport)
#define JNIIMPORT __declspec(dllimport)
#define JNICALL

typedef long jint;
typedef __int64 jlong;
typedef signed char jbyte;


Then run make from gdal/swig/java. and the GDAL DLL is in the path, "java -cp "gdal.jar;build/apps" gdalinfo" should work.

Run the java build

Do a "make" in gdal/swig/java.

You should see a bunch of stuff, possible ending with this:

cp ./.libs/*.so ./
cp: cannot stat `./.libs/*.so': No such file or directory
make: [build] Error 1 (ignored)
Buildfile: build.xml

    [javac] Compiling 33 source files to /wrk/home/warmerda/wrk/gdal-1.5.2/swig/java/build/classes
     [echo] compilation complete

      [jar] Building jar: /wrk/home/warmerda/wrk/gdal-1.5.2/swig/java/gdal.jar

Total time: 2 seconds

Don't worry about the ./.libs/*.so error. That appears to be something that would apply with libtool builds.

Running something

While still in gdal/swig/java try:

javac apps/
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:`pwd`:path_to_directory_where_libgdal_so
java -classpath `pwd`/gdal.jar:`pwd`:`pwd`/apps gdalinfo

Starting with GDAL 1.7.0, the utilities are automatically built in the swig/java/build/apps directory. So after building, you can just do

setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:`pwd`:path_to_directory_where_libgdal_so
java -classpath `pwd`/gdal.jar:`pwd`:`pwd`/build/apps gdalinfo

With luck you will see the gdalinfo usage message.

To deploy you need gdal.jar in your class path, the 4 .so files (,, and and in your shared library path.

Last modified 10 years ago Last modified on Oct 3, 2012, 4:27:29 PM
Note: See TracWiki for help on using the wiki.