My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

BridJ aims to be the ultimate Java / native interoperability library (BSD-licensed).

⇒ Call C, C++, ObjectiveC libraries without compiling native code
FAQ | Download | Change Log | 0.6 API | 0.7-SNAPSHOT API

BridJ is a young project with limitations (in particular with respect to passing structs-by-value, C++ templates and ObjectiveC support...).

It was inspired by JNA but it has :

  • uncompromised speed (thanks to dyncall and assembler optimizations)
  • more features : support for C++, COM, Objective-C...
  • better usability : Java 1.5+ generics, annotations...
  • a liberal BSD license (GPL-compatible)

Key features

  • Dynamic C / C++ / COM interop : call C++ methods, create C++ objects (and subclass C++ classes from Java !)
  • You never need to compile any native code : we deal with the cross-compilation hassle for you once and for all in BridJ ! (works on Windows, Linux, MacOS X, Solaris, Android...)
  • Full JNAerator support : stay away from C / C++ headers !
  • Small library size (~ 600 kB all included)
  • Straightforward type mappings with good use of generics
Learn more

Quickstart

  1. Download Bridj's binaries or add the following repository and dependency to your Maven pom.xml (see the Download page for more options, including specialized trimmed-down versions) :
  2. <dependencies>
      <dependency>
        <groupId>com.nativelibs4java</groupId>
        <artifactId>bridj</artifactId>
        <version>0.6</version>
      </dependency>
      ...
    </dependencies>
    <repositories>
      <repository>
        <id>sonatype</id>
        <name>Sonatype OSS Snapshots Repository</name>
        <url>http://oss.sonatype.org/content/groups/public</url>
      </repository>
      <repository>
        <id>nativelibs4java</id>
        <name>nativelibs4java Maven2 Repository</name>
        <url>http://nativelibs4java.sourceforge.net/maven</url>
      </repository>
      ...
    </repositories>
  3. Generate BridJ wrappers for your library using JNAerator (just select "BridJ" in the "Runtime" combobox).
  4. Read some documentation :
  5. FYI Pointer is probably the most important class to look at, the only other classes you need to know about are the ones created by JNAerator.
  6. Join the NativeLibs4Java Google Group to share your questions and remarks with the community !

Example

Original C++ code :

/// exported in test.dll / libtest.so / libtest.dylib
class MyClass {
public:
    MyClass();
    ~MyClass();
    virtual void virtualMethod(int i, float f);
    void normalMethod(int i);
};
void getSomeCount(int* countOut);
...
void test() {
    int count;
    getSomeCount(&count);
    MyClass t;
    t.virtualMethod(count, 0.5f);
}

Translation + binding with BridJ :

import org.bridj.*;     // C interop and core classes
import org.bridj.ann.*; // annotations
import org.bridj.cpp.*; // C++ runtime
import static org.bridj.Pointer.*; // pointer factories such as allocateInt(), pointerTo(java.nio.Buffer), etc...

@Library("test")
public class TestLibrary {
    static {
        BridJ.register(); // binds all native methods in this class and its subclasses
    }
    public static class MyClass extends CPPObject {
        @Virtual(0) // says virtualMethod is the first virtual method
        public native void virtualMethod(int i);
        public native void normalMethod(int i);
    };
    public static native void getSomeCount(Pointer<Integer> countOut);

    public static void test() {
        Pointer<Integer> pCount = allocateInt();
        getSomeCount(pCount);
        MyClass t = new MyClass();
        t.virtualMethod(pCount.get(), 0.5f);
    }
}
Powered by Google Project Hosting