|
FAQ
BridJ's Frequently Asked Questions
Featured
About the projectWhat is BridJ ?BridJ is a library that lets you call C, C++ and Objective-C libraries from Java as if you were writing native code. It is a recent alternative to JNA. With the help of JNAerator, it provides clean and unambiguous mappings to C/C++ code and a very straightforward and highly typed API to manipulate pointers, classes, structs, unions... It bridges Java's gap when it comes to native-world interoperability.
Read more about : Why another interop library ?In three words : C++, Performance and Usability What is BridJ's license ?BridJ is dual-licensed under a very liberal BSD license and the very liberal Apache 2.0 license. What are BridJ's runtime dependencies ?BridJ is released as a self-contained JAR. However, it includes a copy of the ASM library and its native library are statically linked with a modified version of Dyncall. More details here : CreditsAndLicense. Where can I get support for BridJ ?On NativeLibs4Java's mailing-list. What is BridJ's development status ?Please read the Change Log and have a look at the CurrentState page. What platforms does BridJ support ?Binaries are currently available and routinely tested for the following platforms :
... with plans for FreeBSD (help and / or test hardward welcome) Can I build BridJ from sources ?Sure, please read Build. How can I help ?Please read HowToHelp. How is BridJ different from...JNAThings BridJ aims at doing better than JNA :
import static org.bridj.Pointer.*;
...
Pointer<Integer> p = pointerToInts(1, 2, 3, 4);
p.set(0, 10);
for (int v : p)
System.out.println("Next value : " + v);JNA's advantages over BridJ :
SWIGBridJ's advantages :
SWIG's advantages :
Who is using BridJ ?Please read Who's using BridJ. UsageHow do I bind C / C++ function / struct / class / type XXX with BridJ ?Just paste your C header into JNAerator, choose BridJ as runtime and click on JNAerate. Can I ship my library's binaries in a cross-platform JAR ?You sure can ! BridJ provides a seamless cross-platform binaries JAR extraction mechanism, which it uses for its own needs. For detailed information about embedded libraries, please read Libraries Lookup. How can I trace/log the native calls ?You can set the -Dbridj.logCalls=true property or the BRIDJ_LOG_CALLS=1 environment variable. In this log mode :
How to specify different calling conventions for different platforms ?Short answer is : you don't need to. Only Windows x86 has different calling conventions, so @Convention annotations will typically be ignored silently on other platforms. How to I check for errno / GetLastError() ?Simply declare that your function throws LastError, that's it ! You'll get exceptions soon enough ;-) TroubleshootingBridJ hangs with 100% CPU usage !If you're on Windows x86 (or on 64 bits windows in a 32 bits JVM), this is typically a case of bad calling convention being used for a native function or callback binding. Please triple-check the calling convention (could it be stdcall ?) and add a proper @Convention(Convention.Style.xxx) annotation. How do I debug an EXCEPTION_ACCESS_VIOLATION crash ?This crash indicates that the program tried to access memory it wasn't supposed to access (for instance someone read at index 100 in an array of 10 elements). It may also be caused by an invalid calling convention being used, see previous FAQ item) The tricky part here is that this kind of errors usually does not surface immediately : the crash will typically occur in later calls, giving false leads on where the error actually occurred. The only reliable way to debug such errors is to make use of a native debugger. On Windows, Visual C++ comes with a very nice debugger and is available for free in its Express edition (under some conditions). To debug your crash with Visual C++, you should compile your native library in debug mode if possible, then run the java process with the debugger (or attach a debugger to it before any native call is performed). You'll want to turn as many debug checks on as possible in the debugger settings. Also, it will be easier to set the full path to the debug version of your DLL using a manual path override : if your library is named MyLib in BridJ (the value inside the @Library("MyLib") annotation), you can :
BRIDJ_MYLIB_LIBRARY=c:\...\MyProject\Debug\MyLib.dll "-Dbridj.MyLib.library=c:\...\MyLib.dll" You can then check which exact version of your DLL was picked by BridJ by scanning through Visual C++'s "Modules" window. As far as BridJ is concerned, you may try turning the direct mode off with BRIDJ_DIRECT=0 or -Dbridj.direct=false and see if the issue still happens. A last option would be to turn BridJ's "protected" mode on, but right now you'd have to recompile it from sources and uncomment the #define ENABLE_PROTECTED_MODE directive in Exceptions.h. Note that protected mode is currently slow and unstable which is why it's not enabled by default ;-). How do I debug a LoadLibrary : The specified module could not be found error ?If BridJ fails to load a library, it can be that :
I found a bug in BridJ ! Where do I report it ?Getting lots of bug reports is crucial to making BridJ better, so we'd like to encourage you to file issues as you find them without worrying too much about the contents : it's often better for us to have an incomplete report than no report at all, and anyway we'll let you know very politely if your issue comes from a misuse rather than a bug in BridJ. Here's the issue tracker : NativeLibs4Java's Issues on GitHub. And here's a guide on How to Submit Bugs. | |||||
"SWIG is better because :
it's C++ support is much more solid : once it compiles, you're pretty sure it's gonna work (with BridJ"
When calling windows API methods one can get EXCEPTION_ACCESS_VIOLATION by not using the correct calling convention annotation @Convention(Style.StdCall?) on the winapi method.
@CessationOfTime? Thanks for the precision, updated the faq :-) Cheers