Skip to main content

Notes About JNI

Note #0:

How to create JNI:

public class AJNIClass {

static {
System.load("myLib.so");
}

private native static void aMethod (int aParam, String aParamToo, byte[] yetAnotherParam);

}


Note #1:

There are two ways of loading native library:
  1. Using "System.load". This is like loading a file. 
  2. Using "System.loadLibrary". This is like asking JVM loader to search in system path the right library.
    Example: System.loadLibrary("SMJava"); will try to load libSMJava.so in *nix or SMJava.dll in Windoze.
Note #2:

How to create JNI Code:

# javac AJNIClass.java
# javah AJNIClass

It will generate a header. Implement it and compile the arbitary library. Voila! Your program will start to run (of course with bug included).

Note #3:

A pointer in C/C++ is an integer long in the Java. So, you can save pointers in Java. Java will prevent its Garbage Collector from erasing the memory where the data inhabitate. So, be sure to free the memory after using it.

Note #4:

People always use static branch of  a class to load a native. This isn't a bad idea since the library is loaded once when the class is loaded. But, to give you a fairness and control, you could load the native library in any static method. The main method may applicable, this is necessary in times where you want to walk with many platform supported and you may choose which library should be loaded. Oh, with the Singleton around, maybe it can be suitable for non static object. But, I've never tried it before.

Note #5:

There is a God, pray when you see a segmentation fault. :P

Comments

Popular posts from this blog

STAN vs. UI

Ugh, kasihan banget adek gue. Saking kepinteran dia jadi dapet Akuntansi UI dan STAN. Jadi bingung mau masuk yang mana. Beberapa orang (termasuk orang tua gue), menyarankan masuk STAN. Gue malah memperburuk suasana dengan membela memasuki Akuntansi UI, maklum bela almamater. Duh, gue jadi merasa bersalah bikin dia ragu-ragu. Kira-kira enakan masuk mana, yah? Gue juga gak tahu keuntungan masing-masing. Hasil debat sementara: ~ Untuk jangka panjang masuk UI, untuk jangka pendek STAN. ~~Tapi, dia itu kan cewek, ntar pas menikah kemungkinan besar karir terhambat. Eits, ntar, dulu, sekarang kan jamannya emansipasi, bisa aja cowoknya yang jadi BRT. ~ STAN sarang korupsi, kalo masuk STAN jadi pegawai negeri. Kalo mau kaya harus korupsi. Tapi kalo masuk UI, lulus masuk jadi akuntan publik. Sekarang ini, orang membayar akuntan publik untuk memanipulasi nilai pajak dan aset. *SIGH*. Jadi gak ada yang beres ~ dll. Yah, udah gue jadi bingung, apa lagi dia nanya saran gue. Buah, gue gak pengalaman ...

I Hate Marvel Civil War Storyline In Comic

See this snippets from The Amazing Spiderman: [1] http://scans-daily.dreamwidth.org/4625006.html The snippets on [1] made it clear: Stan Lee made Spidey have a strong believe in Privacy. The comic strips show how Spidey even have to face charges because of his anonymity. The accuser even made many accusation to other entities for political attacks. A fan-art/art I've found in the 90's illustrated Spiderman standing in front of Peter Parker tomb. I don't know if that was originally from comic book or fan-made, 90's are a long time ago. That art mesmerized me and introduced me to the importance of privacy. In late nineties, I was joined to a program hold by an NGO. So, at that time I know how crucial a privacy was (and still is) to humanity. I'm not exaggerating! Humanity would fall to big financial organizations if people could not voice their fears in anonymity. Whistleblowers around the world would not dare to come up. We would not see any suppression ge...