Forum > General
Java class to access fpc library ?
Fred vS:
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: ---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();
...
}
}
--- End code ---
I would do a separate class like that, that could be accessed by other Java application :
--- Code: ---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");}
...
}
--- End code ---
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
Fred vS:
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. ;)
jmpessoa:
Hi Fred,
Maybe you can adjust this example from Lazarus Android Wizard demo...
--- Code: ---
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();
}
}
}
--- End code ---
You can use that wrapper inside the other java applications/main....
--- Code: ---
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...
}
}
--- End code ---
EDIT....
Pascal Code:
--- Code: ---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.
--- End code ---
Fred vS:
--- Quote ---=> I will update the wiki (=> Tricks to do fpc-native-library Java compatible).
--- End quote ---
=> Done => http://wiki.freepascal.org/shared_library => See end of page...
Fred vS:
@ jmpessoa => Oops, we post together at same time...
Whaw, many thanks, trust me that i will deeply study your code. :-X
Many thanks.
Fred
Navigation
[0] Message Index
[#] Next page