Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you go.</p> <p><strong>Building the lib</strong><br> You probably know that jni libs on the mac must be named in the form libFoo.jnilib. You then load the lib from Java by calling System.loadLibrary("Foo");<br> 1. Create a new dynamic library project. Name it libSomething. That's half the naming battle.<br> 2. Search the target build settings for Executable Extension. By default this is set to dylib. Modify it to be jnilib. That's the second half of the naming battle.<br> 3. Add the JavaVM framework to the linked frameworks. This has the JNI stuff in it.<br> 4. Generate your native header file as usual.<br> 5. Include it in your project and create a corresponding .m file.<br> 6. You have to modify the include path for jni.h. On the mac this is <code>&lt;JavaVM/jni.h&gt;</code> <strong>or</strong> use the JavaNativeFoundation framework. I recommend the latter.<br> 7. (optional) The JavaNativeFoundation provides a number of macros and methods that will make converting data/methods to/from Java/Objective-C a lot easier. It's not well documented, but it's worth investigating. If you decide to use JNF, use the open triangle on the JavaVM framework. You will see that JNF is a subframework. Drag it up to the top level equal to JavaVM. Then you can start using it. import <code>&lt;JavaNativeFoundation/JavaNativeFoundation.h&gt;</code> Do a Google search on JNF_COCOA_ENTER to see examples of how to use JNF </p> <p>You should now be able to build a valid jni library. Assuming you have no bugs in your code, you're done. ;-)</p> <p><strong>Debugging</strong><br> I tried everything I found on various web pages to get debugging JNI under Xcode to work. It sort of worked, but would hang when stepping over cocoa calls. However, debugging with gdb <strong>does</strong> work. It's really not all that bad if you run gdb within emacs. The drawback, of course, is you'll need to come up to speed on emacs and gdb, if you aren't already. Here's the gist:<br> 1. Compile your lib with debugging symbols<br> 2. Set a breakpoint in your java calling code after the lib has been loaded, but before calling the code you want to debug. Alternatively, you could put a showMessageDialog there. Anything the will block the java program at the right point.<br> 3. Open Activity Monitor and note the pid of your java program.<br> 4. Start emacs. If you are on Lion, the terminal emacs that comes with should work. On SL the terminal emacs doesn't have some of the gdb magic. In both cases, however, I recommend you download and use a mac gui emacs. There's more than one available, I use the one I got <a href="http://emacsformacosx.com/" rel="nofollow">here</a>. If you use emacs in Terminal, you'll want to go to Termina/Preferences/Settings/Keyboard and look down toward the bottom. There's a checkbox "use option as meta key". You'll want that checked.<br> 5. With emacs running press M-x (option x). Type gdb and press enter. You'll see something like "gdb --annotate=2 xxxxxxxxxx". Press backspace until you get to the annotate setting. Enter "pid xxx" where xxx is the pid of your java calling app. You should end up with the line looking like "gdb --annotate=2 pid xxxx". Press enter.<br> 6. You should see gdb loading the symbols for your jni lib.<br> 7. Now press M-x. Type gdb-many-windows. Press enter.<br> 8. You should now see multiple windows in emacs. A gdb command window, local variables, source code, and others.<br> 9. Enter break someFunctionName in the command window. Press Enter.<br> 10. Enter cont in the command window. (This causes execution to continue). Press enter.<br> 11. Now continue in the java debugger, or press enter in the java dialog.<br> 12. You should see gdb hit the breakpoint in your jni code.<br> 13. Study up on emacs and gdb. In particular check out the po command in gdb.<br> 14. And Bob's your uncle. </p> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload