Recent

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

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Java class to access fpc library ?
« on: April 03, 2014, 11:00:49 pm »
Hello.

I know that it is not the good place to ask it.
But... i ask it to many Java forum, to CodeGuru, to StackOverflow,... no answer.
I understand also that, because here it is a Pascal forum, you do not want to see other languages code.
PS : I asked to Code::Block some help to translate a Pascal procedure into a C procedure and i get a PM => "Other languages than C are not allowed, your topic will be deleted..." :o
So if this topic has nothing to do here, please advice, i will destroy it immediately.

Now, the thing.
I have successfully compiled some universal fpc libraries for using it with Java (using jni.pas).

I have done some test with Java code and the link with those native-java-compatible fpc libraries is perfect.

Now i want to create a independent Java Class who will be a wrapper to the fpc library.

But im not a Java programmer and in all the Java doc i find i do not find how they do it.

Here the topic i sent to several Java-forum:
__________________________________________________________

Hello good Java people.
 
I have a very-well working Java application.

That application uses a native library.

That native library uses callback methods from main java application.

I want to create a independent wrapper-class that could be used be other Java applications.

How can i do that and how to code for other application to use that wrapper-class ?

PS : Note that the callback methods from main java application does call to native methods...

Here the working code :
 
Code: [Select]
public class test {

    // The native library declarations. //
// => how to have a independant class ? //
  public static native void nativemethod1(String t);
  public static native void nativemethod2(int i, int j);
  public static native void nativemethod3();
  static {System.loadLibrary("mylib");} 
 ...

// The callback methods used by library //
  public static void method1() // callback used by nativemethod1
   { nativemethod3() ; }
  public static void method2() // callback used by nativemethod2
   { nativemethod1("Hello") ; }
  public static void method3() // callback used by nativemethod3
   { nativemethod1(1,2) ; }
 ...

// The main application //
  public static void main(String[] args)
  {
  nativemethod1();
  nativemethod2();
  nativemethod3();
 ...
  }
}

I  would do a separate class like that, that could be accessed by other Java application :

Code: [Select]
public class TheWrapper {
// The native library declarations. //
// => how to have a independant class ? //
  public static native void nativemethod1(String t);
  public static native void nativemethod2(int i, int j);
  public static native void nativemethod3();
  static {System.loadLibrary("mylib");} 
 ...
}

But how must i do to use that wrapper inside the other java applications ?

Many thanks.
_________________________________________

So if somebody here knows the answer (or where i can find it), i will be very happy.

Many thanks.

Fred
« Last Edit: April 03, 2014, 11:10:58 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #1 on: April 04, 2014, 10:02:03 pm »
Hum, not too much solution...

But, after all, it is the problem of Java developers.  :-X

=> I will update the wiki (=> Tricks to do fpc-native-library Java compatible).

And, for the Java Wrapper Class, Java gurus will do it.  ;)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: Java class to access fpc library ?
« Reply #2 on: April 04, 2014, 10:42:36 pm »
Hi Fred,

Maybe you can adjust this example from Lazarus Android Wizard demo...
 
Code: [Select]

public class JNIHello {

  public native String getString(int flag);
  public native int getSum(int x, int y);

  static {
  try {
      System.loadLibrary("jnihello");
  } catch(UnsatisfiedLinkError ule) {
      ule.printStackTrace();
  }
  }

}

You can use that wrapper inside the other java applications/main....

Code: [Select]

public class App extends Activity {

       //declare....

       JNIHello myHello;


       //use....

@Override
protected void onCreate(Bundle savedInstanceState) {  //main
   
    myHello = new  JNIHello(); //just for demo...

    int sum = myHello.getSum(2,3); //just for demo...

    String  mens = myHello.getString(1); //just for demo...

}

}

EDIT....

Pascal Code:

Code: [Select]
library jnihello;
 
{$mode delphi}
 
uses
  Classes, SysUtils jni;
 
 
function getString(PEnv: PJNIEnv; this: JObject; flag: JInt): JString; cdecl;
begin

    if flag = 1  then
      Result:= (PEnv^).NewStringUTF(PEnv, '1.New message from JNI Pascal')
    else
      Result:= (PEnv^).NewStringUTF(PEnv, '2.New message from JNI Pascal');

end;

function getSum(PEnv: PJNIEnv; this: JObject; x: JInt; y: JInt): JInt; cdecl;
begin
   Result:= x + y;
end;


const NativeMethods:array[0..1] of JNINativeMethod = (
   (name:'getString';
    signature:'(I)Ljava/lang/String;';
    fnPtr:@getString;),
   (name:'getSum';
    signature:'(II)I';
    fnPtr:@getSum;)
);


function RegisterNativeMethodsArray(PEnv: PJNIEnv; className: PChar; methods: PJNINativeMethod; countMethods:integer):integer;
var
  curClass: jClass;
begin
  Result:= JNI_FALSE;
  curClass:= (PEnv^).FindClass(PEnv, className);
  if curClass <> nil then
  begin
    if (PEnv^).RegisterNatives(PEnv, curClass, methods, countMethods) > 0 then Result:= JNI_TRUE;
  end;
end;
 
function RegisterNativeMethods(PEnv: PJNIEnv; className: PChar): integer;
begin
  Result:= RegisterNativeMethodsArray(PEnv, className, @NativeMethods[0], Length(NativeMethods));
end;
 

exports
  getString name 'Java_com_example_appnoguidemo1_JNIHello_getString',
  getSum name 'Java_com_example_appnoguidemo1_JNIHello_getSum';
 
begin

end.
« Last Edit: April 04, 2014, 10:55:07 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #3 on: April 04, 2014, 10:43:30 pm »
Quote
=> I will update the wiki (=> Tricks to do fpc-native-library Java compatible).
=> Done => http://wiki.freepascal.org/shared_library => See end of page...
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #4 on: April 04, 2014, 10:45:12 pm »
@  jmpessoa => Oops, we post together at same time...

Whaw, many thanks, trust me that i will deeply study your code.  :-X

Many thanks.

Fred
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Java class to access fpc library ?
« Reply #5 on: April 05, 2014, 03:57:25 am »
I fix the code, indentation and syntax highlighting. I have crash with the code though, here's the stacktrace:
Quote
Stack: [0xb679c000,0xb67ed000],  sp=0xb67ebf5c,  free space=319k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  [libjvm.so+0x483c7c]  java_lang_String::utf8_length(oopDesc*)+0x1c
V  [libjvm.so+0x4ce158]  jni_GetStringUTFChars+0xa8
C  [libx.so+0x88e4]  P$X_$$_LIBCALLBACK$PJNIENV$POINTER+0x34
j  X.libcallback()V+0
j  X.main([Ljava/lang/String;)V+5
v  ~StubRoutines::call_stub
V  [libjvm.so+0x480885]  JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*)+0x315
V  [libjvm.so+0x6770c9]  os::os_exception_wrapper(void (*)(JavaValue*, methodHandle*, JavaCallArguments*, Thread*), JavaValue*, methodHandle*, JavaCallArguments*, Thread*)+0x19
V  [libjvm.so+0x47f4ff]  JavaCalls::call(JavaValue*, methodHandle, JavaCallArguments*, Thread*)+0x2f
V  [libjvm.so+0x4bc472]  jni_invoke_static(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x242
V  [libjvm.so+0x4d269a]  jni_CallStaticVoidMethod+0xca
C  [libjli.so+0x2e8d]  JavaMain+0x8bd
C  [libpthread.so.0+0x6d78]  start_thread+0xd8

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  X.libcallback()V+0
j  X.main([Ljava/lang/String;)V+5
v  ~StubRoutines::call_stub
This is my JRE info:
Quote
vm_info: Java HotSpot(TM) Server VM (24.51-b03) for linux-x86 JRE (1.7.0_51-b13), built on Dec 18 2013 18:45:30 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: Java class to access fpc library ?
« Reply #6 on: April 05, 2014, 04:26:55 am »
@Leledumbo,

I am Sorry.

The code has no intention of running .... The intention was only to help @Fred find a Model/direction ...

In particular it is necessary to do some work to make sense the following lines:

Code: [Select]
exports
  getString name 'Java_com_example_appnoguidemo1_JNIHello_getString',
  getSum name 'Java_com_example_appnoguidemo1_JNIHello_getSum';

and for Pure Java App I Think we will need this modification:

Code: [Select]

public class App {

      //declare....

      JNIHello myHello;

      //use....

      public static void main(String[] args)
      {
    myHello = new  JNIHello(); //just for demo...

    int sum = myHello.getSum(2,3); //just for demo...

    String  mens = myHello.getString(1); //just for demo...
      }

}


But I think at some point we will be able to produce pure Java Module with Lazarus Android Wizard too....

Thank you.
« Last Edit: April 05, 2014, 05:24:47 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #7 on: April 05, 2014, 04:24:31 pm »
@ Leledumbo and  jmpessoa => many thanks to take care...  ;)

Now, proclamation of the results of the great contest :
Condition to participate => be able to access fpc native library.

=> What language access most easily fpc libraries ?

The Winner:
Python => no need of wrapper, no need of declarations, Python recognizes and deals directly with all native libraries.

The Challengers:
fpc, Delphi, C++, CNet, Basic => all can access native libraries but need some wrappers.

The Looser:
Java => Can access fpc native libraries but needs his "particular" syntax, opaque Class wrapper method... 

 ;)
« Last Edit: April 05, 2014, 04:30:44 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Java class to access fpc library ?
« Reply #8 on: April 05, 2014, 04:33:49 pm »
Er, you will need to declare wrappers with Python if you want to call native library code.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #9 on: April 05, 2014, 04:42:27 pm »
Quote
Er, you will need to declare wrappers with Python if you want to call native library code.

Hum, are you sure, all the tests i have done work without any declaration ?..
Here some test :
https://sites.google.com/site/biotray/fpGUIlib_py.zip
« Last Edit: April 05, 2014, 04:44:02 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Java class to access fpc library ?
« Reply #10 on: April 05, 2014, 04:57:27 pm »
Ah, I see. If you use ctypes module. And it can handle stdcall calling convection, too.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #11 on: April 06, 2014, 12:50:07 am »
Quote
I have crash with the code though, here's the stacktrace:

Hum, could you try this ready-to-use- Java demo ?
For Linux 64 bit and Windows 32 bit.

> => https://sites.google.com/site/biotray/fpGUIlibDemo_Java.zip

-In terminal do:

-> cd /fpGUIlibDemo_Java (-> go in demo path)

-> java -Djava.library.path=. fpg

Here source of native fpc library :

> => https://github.com/fredvs/fpGUIlib

 ;)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Java class to access fpc library ?
« Reply #12 on: April 06, 2014, 03:00:45 am »
Quote
I have crash with the code though, here's the stacktrace:

Hum, could you try this ready-to-use- Java demo ?
For Linux 64 bit and Windows 32 bit.

> => https://sites.google.com/site/biotray/fpGUIlibDemo_Java.zip

-In terminal do:

-> cd /fpGUIlibDemo_Java (-> go in demo path)

-> java -Djava.library.path=. fpg

Here source of native fpc library :

> => https://github.com/fredvs/fpGUIlib

 ;)
Could you build for Linux 32-bit?

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Java class to access fpc library ?
« Reply #13 on: April 06, 2014, 01:23:19 pm »
Quote
Could you build for Linux 32-bit?

Hum, no, i have Linux mint 64 bit (but was never able to do cross-compil  :-X ).
And i do not have virtualizer cpu so, cannot install VitualBox... (really tooooo sloooow)

But it is very easy to compile the library.

- Install fpGUI package =>. https://github.com/graemeg/fpGUI/tree/develop

- Then dowmload fpGUIlib =>  https://github.com/fredvs/fpGUIlib
- Compile in fpGUIlib/src => fpgui.pas. (or fpgui.lpi)

There is a patch for fpc if you have problem of path => fpGUIlib/patch

PS : => Hum, if somebody could compile the 32 bit linux version and give it , as attachment, it will be super  :-[
« Last Edit: April 06, 2014, 01:54:19 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Java class to access fpc library ?
« Reply #14 on: April 06, 2014, 03:04:19 pm »
Err...it's kinda overkill just to test a little piece of code :P

 

TinyPortal © 2005-2018