Lazarus
Free Pascal => General => Topic started 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:
program filedialog;
{$mode objfpc}{$H+}
into =>
library filedialog;
{$mode objfpc}
2) Change:
procedure MainProc;
into =>
procedure MainProc(PEnv: pointer; Obj: pointer); cdecl;
3) At end, add:
exports
MainProc name 'Java_filedialog_mainproc';
end.
=> compile that new fpc library.
The Java application.
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 :
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.
-
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
-
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)
-
Excellent stuff!! :) Keep up the good work.
I'll be trying it out this weekend.
-
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.
-
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