Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-03

Access fpc libraries with Java ?

22 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

fredvs

Hello.
I try to access fpc libraries with Java.
I can access those libraries with:
=> Python without problems, even callback functions... => fpc programs, of course...

But with Java i get trouble...
I try with JNA :

But without luck.

Does somebody already had access to fpc libraries with Java ? So, is it possible ?
If yes, how do you do it ?

Many thanks.
Fred.

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791.html Sent from the Free Pascal - General mailing list .
Unknown author
to fredvs

JNI is the way to go
http://en.wikipedia.org/wiki/Java_Native_Interface JNI is a part of Java, so no other libraries are required to use it

27.03.2014, 19:11, "fredvs" :

Show quoted text 28 lines
Hello. I try to access fpc libraries with Java. I can access those libraries with: => Python without problems, even callback functions... => fpc programs, of course... But with Java i get trouble... I try with JNA : >>  https://github.com/twall/jna But without luck. Does somebody already had access to fpc libraries with Java ? So, is it possible ? If yes, how do you do it ? Many thanks. Fred. ----- Many thanks ;-) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
leledumbo
to fredvs: Hello. I try to access fpc libraries with Java. I can access those libraries with: =>…
fpcside:
// fpclib.pas
library fpclib;
procedure test(env,jcls: pointer; i: longint); cdecl;
begin
  writeln(i);
end;
exports
  test name 'Java_TestFPC_test';
end.
java side:
// TestFPC.java
public class TestFPC {
  public static native void test(int i);

public static void main(String[] args) {

    System.loadLibrary("fpclib");
    test(255);
  }

}

put both under the same directory, compile each, and execute with: java -Djava.library.path=. TestFPC

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718794.html Sent from the Free Pascal - General mailing list .
fredvs
to leledumbo

Yep, yep, yep, ... tetra * mega thanks.

Hum, i have very poor knowledge with Java. Of course i can do a "Hello World" and some other things.
But now, because of some fpc libraries, i explore the "import native library" part of java.
And, what surprise me, in Java forum, was that lot of Java programmers want to create their own library for use by other Java programmers, into Java programs.

And for them there is only one solution : create a C library. It exists tools to create the C header, but for creating their library, they must know C.
And that is the problem, most of Java programmers are afraid about C (like everybody ;-) ).

Java has a big % part of the programmers, so the question is : => Why not propose to those poor Java developers who need to create a Java compatible native library to do it... with fpc.

Other question:

=> I know that it is a difficult for fpc to communicate with Java-Android. Would it not be easier to interface via fpc libraries (who will do the hard work) and let the final procedures done by Java ?

=> Leledumbo and Hint => Re-thanks-thanks-thanks. ;-)

Fred

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718795.html Sent from the Free Pascal - General mailing list .
fredvs
to leledumbo: fpcside: // fpclib.pas library fpclib; procedure test(env,jcls: pointer; i: longint);…

Yep Leledumbo, this time you kill me.

Your code is working PERFECTLY and trust me, , they do not know your trick, there are lot of questions for that without any working answer.

Your GENIUS trick is to export name => Java_MyLib_MyProcedure. I do not know if that could be done with C libraries but here i have a HUGE argument to convert Java developers who need a native library to use fpc instead of C.
And for us, fpc developers it is not difficult to adapt the code to be Java full-easy compatible.

Leledumbo, i am your fan, forever.

Many thanks.

Fred

PS: Hum, why did you use
=> env,jcls: pointer;
as arguments, is it needed for Java ?
I have try without and it works too...

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718796.html Sent from the Free Pascal - General mailing list .
Sven Barth
to fredvs
fredvs wrote 8 lines
Your code is working PERFECTLY and trust me, , they do not know your trick, there are lot of questions for that without any working answer. Your GENIUS trick is to export name => Java_MyLib_MyProcedure. I do not know if that could be done with C libraries but here i have a HUGE argument to convert Java developers who need a native library to use fpc instead of C.

Then those users don't know where to look, because even Wikipedia documents this: http://en.wikipedia.org/wiki/Java_Native_Interface And then there are even specific tutorials if you look for "JNI tutorial" and maybe some language name and with the advent of Android this got even more, because this is the way how you interface native with Java code there...

Regards,
Sven

leledumbo
to fredvs: Yep Leledumbo, this time you kill me. Your code is working PERFECTLY and trust me, , they…
PS: Hum, why did you use => env,jcls: pointer; as arguments, is it needed for Java ? I have try without and it works too...

It's JNI requirement, I wonder it could work without as it may corrupt the stack. The first parameter is the JVM environment, the second is the Java object (or class in case of static method), the rest is actual parameters to the function.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718799.html Sent from the Free Pascal - General mailing list .
Helmut Weber
to fredvs: Yep, yep, yep, ... tetra * mega thanks. Hum, i have very poor knowledge with Java. Of…

I don't understand you argument.
Java is quite similar to C or C++.
So who develops in Java and wants to import a native library would use C or C++, not Pascal.

Best,
Helmut

Earlier message 51 lines
----- Original Message ----- From: "fredvs" To: Sent: Thursday, March 27, 2014 10:18 PM Subject: Re: [fpc-pascal] Access fpc libraries with Java ? > Yep, yep, yep, ... tetra * mega thanks. > > Hum, i have very poor knowledge with Java. Of course i can do a "Hello > World" and some other things. > But now, because of some fpc libraries, i explore the "import native > library" part of java. > And, what surprise me, in Java forum, was that lot of Java programmers > want > to create their own library for use by other Java programmers, into Java > programs. > > And for them there is only one solution : create a C library. > It exists tools to create the C header, but for creating their library, > they > must know C. > And that is the problem, most of Java programmers are afraid about C (like > everybody ;-) ). > > Java has a big % part of the programmers, so the question is : > => Why not propose to those poor Java developers who need to create a Java > compatible native library to do it... with fpc. > > Other question: > > => I know that it is a difficult for fpc to communicate with Java-Android. > Would it not be easier to interface via fpc libraries (who will do the > hard > work) and let the final procedures done by Java ? > > => Leledumbo and Hint => Re-thanks-thanks-thanks. ;-) > > Fred > > > > ----- > Many thanks ;-) > -- > View this message in context: > http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718795.html > Sent from the Free Pascal - General mailing list . > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Helmut Weber
to Mattias Gaertner

For me the Java Programming Language (J. Gosling, Sun, Oracle) seems to me quite uniquely defined. As a developper I personally use the compiler Oracle JVM 1.7.0_03 for Windows.

Helmut

Earlier message 19 lines
----- Original Message ----- From: "Mattias Gaertner" To: Sent: Friday, March 28, 2014 10:18 AM Subject: Re: [fpc-pascal] Access fpc libraries with Java ? > On Fri, 28 Mar 2014 09:55:47 +0100 > "Helmut Weber" wrote: > >> Java is quite similar to C or C++. > > What kind of Java are you talking about? > > > Mattias > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
fredvs
to leledumbo: It's JNI requirement, I wonder it could work without as it may corrupt the stack. The…
>It's JNI requirement, I wonder it could work without as it may corrupt the

stack.

>The first parameter is the JVM environment, the second is the Java object > (or class in case of static method), the rest is actual parameters to the > function.

Yep, you are right, it was working only for function without argument (but i use now your trick even for procedure without parameters.).

I have successfully adapt the libraries for Java and im really impressed by the result (mainly for fpGUI and uos).

Hum, have you a idea how to deal with Pchar and Callback-Procedure ? How to declare in java :

> Procedure MyProcPchar(MyPchar : Pchar) ;

and

> Type TProc : procedure; > Procedure MyProcTproc(MyProc : TProc);

Many thanks.

> Procedure ProcProc(

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718807.html Sent from the Free Pascal - General mailing list .
fredvs
to Sven Barth: Then those users don't know where to look, because even Wikipedia documents this:…
And then there are even specific tutorials if you look for "JNI tutorial" and maybe some language name and with the advent of Android this got even more, because this is the way how you interface native with Java code there...

Hum, have you really test those tutorials ?
You must be a very good guru to understand JNI examples.

And if you see the example from Leledumbo it is 1.000 times more clear, logical and easy.
Sorry, but i still think that it would be much easier for Java developers to create-link fpc libraries than C libraries...

Fred.

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718808.html Sent from the Free Pascal - General mailing list .
leledumbo
to fredvs: stack. Yep, you are right, it was working only for function without argument (but i use…
Hum, have you a idea how to deal with Pchar and Callback-Procedure ?

Welcome to real JNI programming :)
For this, you can no longer ignore the first 2 parameters. PChar can't be passed to Java AFAIK (it will violate Java safety rule). However, there's another way around: expect a jstring instead and fill/return the jstring from Pascal (you'll have to use first parameter methods). Callback is possible by passing object implementing required method. All in all, read up JNI tutorial and find JNI wrapper for Pascal. Custom Drawn & Android Module Wizard has JNI wrapper, but they develop each independently and I have no idea which one is better. Choose wisely.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718809.html Sent from the Free Pascal - General mailing list .
fredvs
to leledumbo: Welcome to real JNI programming :) For this, you can no longer ignore the first 2…
> Custom Drawn & Android Module Wizard has JNI wrapper, but they develop

each independently

> and I have no idea which one is better. Choose wisely.

Yes, but sadly, both do not implement the JStringToString() function from the original Delphi jni.pas.

OK, i will try to implement JStringToString() function (or find the conversion-solution)...
PS : Advices are welcome...

Thanks.

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718814.html Sent from the Free Pascal - General mailing list .
fredvs
to fredvs
> OK, i will try to implement JStringToString() function (or find the

conversion-solution)...

Done =>

function JStringtoString(PEnv: PJNIEnv; Obj: JObject; JavaStr: JString) :
String;
var
 ifcop : pjboolean;
begin
result  := (PEnv^).GetStringUTFChars(PEnv, JavaStr, ifcop);
end;

He, he, he, ... now the last obstacle : Callback procedures... and Java is in the pocket for fpc libraries...

PS => lot of things to add in fpc wiki ;-)

Fred

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718821.html Sent from the Free Pascal - General mailing list .
fredvs
to fredvs
> now the last obstacle : Callback procedures...

Hum, i do not find why it is not working ;-(

I follow jni c examples but with no luck.

Does somebody knows how to deal with callback procedures and Java ?

Here Library Pascal code :

procedure formonclick(PEnv: PJNIEnv; Obj: JObject; aproc: JString); cdecl;
begin
JMethod :=
(PEnv^^).GetStaticMethodID(PEnv,(PEnv^^).GetObjectClass(PEnv,Obj),(PEnv^^).GetStringUTFChars(PEnv,
aproc, nil),'()V')  ;
end;

And here Java code :

public class fpg {

public native void register();
public void callback() {
writeln("Ýep it works");
}
 public static native void buttononclick(int i, int j, String t);

public static void main(String[] args) {

    System.loadLibrary("fpg");
     formonclick("callback");
    }

}

fpc Library and java app compiles but when running the java app i get that error :

>Exception in thread "main" java.lang.NoSuchMethodError: callback1 > at fpg.formonclick(Native Method) at fpg.main(fpg.java:34)

What is wrong ? => I do not understand because callback is declared in main java thread..

Any help would be extremely appreciated.

Many thanks.

Fred.

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718829.html Sent from the Free Pascal - General mailing list .
Alan Krause
to fredvs

Here is a somewhat dated version of JNI.pas <http://pastebin.com/DR3px8GT> for FPC which might help you out.

Alan

fredvs wrote 5 lines
>> now the last obstacle : Callback procedures... Hum, i do not find why it is not working ;-( I follow jni c examples but with no luck.
leledumbo
to Alan Krause
Here is a somewhat dated version of JNI.pas for FPC which might help you

out.

So now we have 3 JNI.pas wrapper...
I don't see any disclaimer, doc, changelog, whatsoever. Where do you get it? Also, if you don't mind, update the dirty

{$ifdef mswindows}stdcall;{$else}cdecl;{$endif}

with:

{$macro on}
{$ifdef mswindows}
{$define jnicall:=stdcall}
{$else}
{$define jnicall:=cdecl}
{$endif}

once before all declarations, and you can simply use jnicall; as calling convention modifier

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718831.html Sent from the Free Pascal - General mailing list .
Alan Krause
to leledumbo

I have absolutely *no* idea where I got it - only that it works as advertised.

Alan

------------------------------
Alan L. Krause of Software Development*Voice:
(760) 634-1700Sherman and Associates, Inc. <http://www.shermanloan.com/>Fax: (760) 634-1992

leledumbo wrote 21 lines
> Here is a somewhat dated version of JNI.pas for FPC which might help you out. So now we have 3 JNI.pas wrapper... I don't see any disclaimer, doc, changelog, whatsoever. Where do you get it? Also, if you don't mind, update the dirty {$ifdef mswindows}stdcall;{$else}cdecl;{$endif} with: {$macro on} {$ifdef mswindows} {$define jnicall:=stdcall} {$else} {$define jnicall:=cdecl} {$endif} once before all declarations, and you can simply use jnicall; as calling convention modifier
fredvs
to Alan Krause

@ Alan and Leledumbo : Many thanks.

@ Alan, i have try with your jni.pas but... i loose.

The only thing that could save my life is.. a working simple example of a fpc library with only one export procedure.
That procedure will do a callback of a Java procedure.:.

=> simple fpc library code + simple java code => A new useful well documented wiki ;-)

Many thanks.

Fred.

-----
Many thanks ;-)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Access-fpc-libraries-with-Java-tp5718791p5718833.html Sent from the Free Pascal - General mailing list .