[fpc-pascal] JNI/Android: Java events calling pascal code

Chriss Kalogeropoulos iz.iznogood at gmail.com
Wed Apr 23 12:20:33 CEST 2014


Hello everyone,

a couple years ago i tried the same thing. The task was to implement a
JavaPOS driver handler from Delphi/FPC code.
The actual driver was implemented in C++ and it used a Java interface. My
code was the glue between the application (Delphi/FPC) and the driver, it
had to hook the events and call the appropriate methods for printing etc
(it was a POS printer driver).

I found that the only way to hook a native implemented listener to the java
class was either to write a small piece of code in java to do the
dispatching to my code OR use dynamic compilation to create the Java byte
code. Basically i had to write the java code inside my dll (in a constact
string) and pass this to the Java compiler to create the byte code in
memory.
This needed a lot of work and i dropped it since the actual java code was
less than 30 lines and it was very generic, so it was not worth the effort.
Check this
http://www.javablogging.com/dynamic-in-memory-compilation/

This needs Java 6 (which was not an option in my case, i had to use older
versions). It might help you.
Also it needs to store the source files locally otherwise more dynamic
class generation will be needed.
The link above has the info you need.

Chriss



On Wed, Apr 23, 2014 at 12:31 PM, Felipe Monteiro de Carvalho <
felipemonteiro.carvalho at gmail.com> wrote:

> On Wed, Apr 23, 2014 at 6:10 AM, leledumbo <leledumbo_cool at yahoo.co.id>
> wrote:
> > Hmm...that's beyond my Java skill. Since Felipe who started the topic,
> you
> > can try PM him instead.
>
> So looking at what he originally posted:
>
> setOnCompletionListener(MediaPlayer.OnCompletionListener listener)
>
> And looking into the documentation:
>
>
> http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
>
> you need to provide a class which implements the
> MediaPlayer.OnCompletionListener interface:
>
> There is nothing in JNI unfortunately which would allow us to create
> new classes. JNI is very limited, unlike objc runtime which gives us
> access to everything, JNI gives us a limited access to the Java world.
>
> I already asked in the past in Java/Android groups about this, how to
> get rid of Java when you need to pass a class which implements and
> interface.
>
> The answer is that it is possible: You need to create the class in
> ...... java bytecode =D And then pass it to Java.
>
> At this point I gave up, considering it too awkward and wrote my
> software part in Java and part in Pascal.
>
> Ideally it would not be so hard if we had a bytecode generator which
> accepts as input things like classname, which interfaces it will
> implement, list of functions and address to their implementation, etc,
> etc. But we don't have at the moment AFAIK.
>
> So maybe someone braver will actually do this, since it is possible =D
> But I didn't for my Android app in the app store (True Democracy)....
>
> --
> Felipe Monteiro de Carvalho
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140423/1c99d8d5/attachment.html>


More information about the fpc-pascal mailing list