[Original] Linux write local method

xiaoxiao2021-03-06  78

[Original] Linux write local method creation local method has 7 steps: • Write Java code • Compile Java code • Create a C header file (.h file) • Create a C stubs file • Write C code • Create a shared codebook or DLL • Run the program

Write Java code

In your code, use your local method, you need to do two things, first, write a local method declaration for each local method you want. Just like writing a Java method interface, but you must use the native keyword to indicate this is a local method:

Public native void printText ();

In the second step, you must make a clear load on the local code base, we are loaded in the Static block in a class: static {system.loadLibrary ("happy");}: Class Happy {public native void printText () Static {system.loadLibrary ("happy"); / * Note LowerCase of classname! * /} Public static void main (string [] args) {happy happy = new happy (); happy.printtext ();}} Java code Javac happy.java creates a C header file javah happy creation C stubs file javah -stubs happy write c code #include / * standard native method stuff. * / #Include "happy.h" / * generated earlier. * / #Include / * Standard C Io stuff. * / Void Happy_PrintText (Struct Hhappy * this) {PUTS ("Happy New Year !!!");} Create a shared library First, compile we already have C files. You must tell the compiler to find the Java local method support file, but the main thing is that you must tell the compiler to produce a location independent code: gcc -i / usr / local / java / include -i / usr / local / java / include / genunix -fpic -c happy.c happyimp.c Now create a shared library: gcc -shared -wl, -soname, libhappy.so.1 -o libhappy.so.1.0 happy.o Happyimp.o cp libhappy.so.1.0 libhappy.so export ld_library_path = `pwd`: $ ld_library_path Perform application Java Happy

转载请注明原文地址:https://www.9cbs.com/read-121075.html

New Post(0)