Lazarus

Free Pascal => General => Topic started by: Fred vS on September 16, 2014, 02:49:16 pm

Title: Java application := fpc application !
Post by: Fred vS on September 16, 2014, 02:49:16 pm
Hello.
There is a hypra-simple way to convert all your fpc applications into Java applications !

It is working for all fpc console-applications.

Here how to do (using the fpGUI demo /fpGUI/examples/gui/filedialog.lpr).

The original fpc program:
1) Change:
Code: [Select]
program filedialog;
 {$mode objfpc}{$H+}
into =>
Code: [Select]
library filedialog;
{$mode objfpc}

2) Change:
Code: [Select]
procedure MainProc;into =>
Code: [Select]
procedure MainProc(PEnv: pointer; Obj: pointer); cdecl;
3) At end, add:
Code: [Select]
exports
MainProc name 'Java_filedialog_mainproc';
end.

=> compile that new fpc library.


The Java application.
Code: [Select]
public class filedialog {
public static native void mainproc();
public static void main(String[] args)
  {
 System.loadLibrary("filedialog");
  mainproc();
 }
 }

Save it as filedialog.java in same directory.

And to run the Java application, do, in terminal :

Quote
java -Djava.library.path=. filedialog

And... IT WORKS ;-) see that :=>

>> https://sites.google.com/site/fiensprototyping/fpgui_java.jpg (https://sites.google.com/site/fiensprototyping/fpgui_java.jpg)

Fred.
Title: Re: Java application := fpc application !
Post by: felipemdc on September 16, 2014, 04:14:00 pm
Yes, it's called JNI and we have some docs about it:

http://wiki.freepascal.org/Android_Programming#Table_of_Java_types_and_Pascal_JNI_types
Title: Re: Java application := fpc application !
Post by: Fred vS on September 16, 2014, 08:22:57 pm
Quote
Yes, it's called JNI

Are you sure?  Do you have noticed that it uses one and only one procedure to run all the (complicated) fpc application, without jni.pas in uses section?  :-X

Here sources + binaries (original program and library-Java-compatible) of filedialog fpGUI demo.
The binaries are compiled on Linux 64 bit.
>> https://sites.google.com/site/fiensprototyping/fpguitest_java.tar.gz  (https://sites.google.com/site/fiensprototyping/fpguitest_java.tar.gz)
Title: Re: Java application := fpc application !
Post by: Graeme on September 19, 2014, 02:21:16 pm
Excellent stuff!!  :) Keep up the good work.

I'll be trying it out this weekend.
Title: Re: Java application := fpc application !
Post by: felipemdc on September 20, 2014, 01:59:01 pm
Are you sure?

Yes, I am sure. Your requisite is very simple (just run 1 parameterless procedure), so that's why you can get away with using Pointer instead of the types in jni.pas. If you wanted more java functionality (like sending data back and forth) then you would need the full jni.pas unit.

Anyway, what is the use for this? If you don't need anything from Java anyway I don't see the point.
Title: Re: Java application := fpc application !
Post by: Fred vS on September 20, 2014, 02:31:09 pm
Quote
Anyway, what is the use for this? If you don't need anything from Java anyway I don't see the point.

Dear Felipe, could you take a look here (and see who made it)  :-X =>
http://wiki.freepascal.org/Using_Pascal_Libraries_with_Java (http://wiki.freepascal.org/Using_Pascal_Libraries_with_Java)

With some OS (like Android), you may only run Java applications.

The tip of this topic gives the opportunity to all fpc-console developers to transform their (complicated) applications into  working Java applications.  Without the need to deal with Jni.pas. And with only 3 lines of code changed.

By the way, i highly appreciate all the work you have done for Jni and, like you, i think that the couple Java-fpc will have great days.

Thanks.

Fred