Recent

Author Topic: Java class to access fpc library ?  (Read 9728 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 1672
    • miXimum is the DJ's best friend
Re: Java class to access fpc library ?
« Reply #15 on: April 06, 2014, 07:57:39 pm »
Quote
Err...it's kinda overkill just to test a little piece of code :P

Ok, i agree...  :-[

Hum, i think the problem comes from :

Code: [Select]
theclass := (PEnv^^).FindClass(PEnv,'MyCallbackClass') ; ///// => the problem
'MyCallbackClass' must be a JString.

In the demo i use one more native procedure in the library: javainit:

Code: [Select]
library mycallbacklib;
 
uses
  jni;
...
var
theclass : JClass;  //// => This added...
...
procedure JavaInit( PEnv: PJNIEnv; Obj: JObject ; MClass : Jstring); cdecl;
var
MainClass : Pchar ;
begin
 MainClass :=  (PEnv^^).GetStringUTFChars(PEnv, MClass, nil); /// Here MClass is Jtring
 theclass := (PEnv^^).FindClass(PEnv,MainClass) ;
 (PEnv^^).ReleaseStringUTFChars(PEnv, MClass, nil);
end; 

procedure libcallback(PEnv: PJNIEnv; Obj: JObject); cdecl;
var
  theproc  : PChar;
  theMethod: JMethodID;
begin
  theproc := (PEnv^^).GetStringUTFChars(PEnv, 'callback', nil);
  theMethod := (PEnv^^).GetStaticMethodID(PEnv, theclass, theproc,'()V')  ;
  (PEnv^^).CallVoidMethod(PEnv,Obj,theMethod) ; // run the callback...
end;
...
exports
...
 libcallback name 'Java_MyCallback_libcallback',
JavaInit name 'Java_MyCallback_javainit;
...
begin
end.

And in Java code :

Code: [Select]
public class MyCallback {
  public static void callback() {
    System.out.println("Yep, it works...");
  }
 public static native void javainit(String st); //// => this is added
  public static native void libcallback();
 
  public static void main(String[] args) {
    System.loadLibrary("mycallbacklib");
  javainit("MyCallback");
    libcallback();
  }
}

« Last Edit: April 07, 2014, 07:44:02 pm by Fred vS »
I use Lazarus 1.8.0 32/64 and FPC 3.0.3 32/64 on Linux Mint Mate 17 32/64, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs

jmpessoa

  • Hero Member
  • *****
  • Posts: 1455
Re: Java class to access fpc library ?
« Reply #16 on: April 06, 2014, 08:01:53 pm »
Hi Fred,

You can change

public static void javainit(string st); //// => this is added

to

public static void javainit(String st); //// => java is case sensitive

thank you.
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Fred vS

  • Hero Member
  • *****
  • Posts: 1672
    • miXimum is the DJ's best friend
Re: Java class to access fpc library ?
« Reply #17 on: April 06, 2014, 08:06:25 pm »
@ : jmpessoa : done y obrigado... ;)
I use Lazarus 1.8.0 32/64 and FPC 3.0.3 32/64 on Linux Mint Mate 17 32/64, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs