Somwhere in the bowels of the InterWeb, two coders converse:

2:07:12 AM qphoebusq: so
2:07:16 AM qphoebusq: i’m trying to use JNI
2:07:30 AM qphoebusq: which at first I thought was a way to call c++ functions directly from a library
2:07:43 AM joshkimdotorg: JNI?
2:07:45 AM qphoebusq: so you have a compiled native code library, a .dll on windows or a .so on linux or whatever
2:07:49 AM joshkimdotorg: NI?
2:07:49 AM qphoebusq: Java Native Interface
2:07:51 AM joshkimdotorg: ah ha
2:08:01 AM qphoebusq: and the idea is
2:08:10 AM qphoebusq: to be able to call routines from that library
2:08:16 AM qphoebusq: but it turns out i think
2:08:37 AM qphoebusq: that I would have to re-implement the library using special “name-mangled” types
2:08:46 AM qphoebusq: i’m not kidding, name-mangled is the official term
2:08:51 AM joshkimdotorg: lol
2:08:51 AM qphoebusq: like “we know this is stupid”
2:09:06 AM qphoebusq: now, a cross-language interface like that would be elegant
2:09:23 AM qphoebusq: i have my compiled c++ compression library, and my java GUI app that calls it
2:09:27 AM qphoebusq: but i don’t have that kind of time
2:09:30 AM qphoebusq: so i’m going to hack it
2:09:36 AM joshkimdotorg: tee hee
2:09:45 AM qphoebusq: and write the compressor as a console application
2:09:51 AM qphoebusq: and then call it with System.exec
2:09:54 AM qphoebusq: from the java program
2:09:58 AM qphoebusq: with the proper command line args
2:10:06 AM qphoebusq: is that so wrong?
2:10:23 AM joshkimdotorg: no
2:10:24 AM joshkimdotorg: java is wrong
2:11:01 AM qphoebusq: exactly
2:11:03 AM qphoebusq: so wrong
2:11:10 AM qphoebusq: if java was friggin compiled
2:11:17 AM qphoebusq: then i wouldn’t need this bs JNI interface
2:11:21 AM qphoebusq: b/c native code is native code
2:11:27 AM qphoebusq: regardless of its original language
2:11:44 AM qphoebusq: but the JVM forces me to do this cross-language bridge bs if i want to call native methods directly
2:12:10 AM joshkimdotorg: what the freak
2:14:50 AM qphoebusq: look at this simple function
2:14:53 AM qphoebusq: converted to JNI
2:14:55 AM qphoebusq:

#include <jni.h>
extern "C" JNIEXPORT void JNICALL
Java_UseObjects_changeObject(
JNIEnv* env, jobject, jobject obj) {
  jclass cls = env-&gt;GetObjectClass(obj);
  jfieldID fid = env-&gt;GetFieldID(
    cls, "aValue", "I");
  jmethodID mid = env-&gt;GetMethodID(
    cls, "divByTwo", "()V");
  int value = env-&gt;GetIntField(obj, fid);
  printf("Native: %dn", value);
  env-&gt;SetIntField(obj, fid, 6);
  env-&gt;CallVoidMethod(obj, mid);
  value = env-&gt;GetIntField(obj, fid);
  printf("Native: %dn", value);
} ///:~

2:15:01 AM qphoebusq: java shot my C++
2:16:15 AM joshkimdotorg: HAHAHA
2:16:33 AM joshkimdotorg: what the freak is that supposed to be?
2:16:44 AM qphoebusq: it reads the fields in an object
2:16:48 AM qphoebusq: calls a method
2:16:50 AM qphoebusq: and print
2:16:51 AM qphoebusq: s
2:16:54 AM joshkimdotorg: what’s the method name?
2:17:20 AM qphoebusq: changeObject
2:17:27 AM joshkimdotorg: oh I get it
2:17:28 AM qphoebusq: just testing stuff
2:17:30 AM qphoebusq: yeah
2:17:34 AM qphoebusq: isn’t it disgusting
2:17:35 AM joshkimdotorg: what the freaking dumb
2:21:32 AM qphoebusq: it makes bjorn stroustrup cry
2:21:51 AM joshkimdotorg: it makes him want to kick sun in the face
2:22:52 AM qphoebusq: that would be sweet
2:23:00 AM qphoebusq: just go down there and *bam*
2:23:11 AM qphoebusq: beat them over the head with a copy of “Programming JNI” or something
2:24:45 AM joshkimdotorg: hehe
2:27:41 AM qphoebusq: “i’m gonna make bytecode outta your ass”
2:27:50 AM joshkimdotorg: haha
2:27:55 AM joshkimdotorg: don’t make me compile you
2:28:04 AM joshkimdotorg: into BITS
2:28:15 AM joshkimdotorg: that’s gotta go on the blog
2:28:17 AM joshkimdotorg: go, copy and paste
2:30:09 AM qphoebusq: roflol
2:30:55 AM qphoebusq: i’m just gonna post a transcript
2:30:56 AM qphoebusq: k?

The guy I’m chatting with…


No Comments on “JNI, a tricksy beast it is”

You can track this conversation through its atom feed.

No one has commented on this entry yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>