publicinterfaceCHelloWorldextendsLibrary { // On charge la lib CHelloWorldINSTANCE= Native.load("helloworld.so", CHelloWorld.class); // on décrit l'interface utilisée Pointer hello(String g); // Youpi, on pourra appeler free voidfree(Pointer p); // JNA le fournit (et pas que ça) }
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassTest { publicstaticvoidmain(String[] args) { Pointerptr=null; try { // on appelle notre fonction C ptr = CHelloWorld.INSTANCE.hello(args[0]); // on parse le résultat System.out.println(ptr.getString(0)); } catch (Exception e) { throw e; } finally { // On libère la mémoire if (null != ptr) { CHelloWorld.INSTANCE.free(ptr); } } } }