Oggi sono alle prese con i metodi nativi ed il manuale mi propone i seguenti codici e spiega come usarli ma non si sofferma sull'utilizzo di Eclipse e di altri eventuali programmi per la creazione dei singoli file di interscambio. Qualcuno mi può aiutare specificando per filo e per segno le operazioni da compiersi?
listing A
listing Bcodice:// A simple example that uses a native method. public class NativeDemo { int i; public static void main(String args[]) { NativeDemo ob = new NativeDemo(); ob.i = 10; System.out.println("This is ob.i before the native method:" + ob.i); ob.test(); // call a native method System.out.println("This is ob.i after the native method:" + ob.i); } // declare native method public native void test() ; // load DLL that contains static method static { System.loadLibrary("NativeDemo"); } }
listing Ccodice:/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class NativeDemo */ #ifndef _Included_NativeDemo #define _Included_NativeDemo #ifdef _ _cplusplus extern "C" { #endif /* * Class: NativeDemo * Method: test * Signature: ()V */ JNIEXPORT void JNICALL Java_NativeDemo_test (JNIEnv *, jobject); #ifdef _ _cplusplus } #endif #endif
Sul manuale c'è scritto che prima di tutto bisogna creare NativeDemo.h usando il codice javah -jni NativeDemo e caricarlo con il C ma se scrivo il listato A in Eclipse ottengo il seguente errore:codice:/* This file contains the C version of the test() method. */ #include <jni.h> #include "NativeDemo.h" #include <stdio.h> JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *env, jobject obj) { jclass cls; jfieldID fid; jint i; printf("Starting the native method.\n"); cls = (*env)->GetObjectClass(env, obj); fid = (*env)->GetFieldID(env, cls, "i", "I"); if(fid == 0) { printf("Could not get field id.\n"); return; } i = (*env)->GetIntField(env, obj, fid); printf("i = %d\n", i); (*env)->SetIntField(env, obj, fid, 2*i); printf("Ending the native method.\n"); }
codice:Exception in thread "main" java.lang.UnsatisfiedLinkError: no NativeDemo in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at NativeDemo.<clinit>(NativeDemo.java:17)![]()
![]()
![]()
Mai più un testo di Herbert Schildt...![]()

Rispondi quotando
