-
April 2nd, 2014, 10:38 AM
#1
Create and use a wrapper class in java
Hello ;-)
I have a very-well working Java application.
That application uses a native library.
That native library use callback methods from main java application.
I want to create a independent wrapper-class that could be used be other 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 do call to native methods...
Code:
Here the working 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();
...
}
}
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");}
...
}
But how must i do to use that wrapper inside the other java applications ?
Many thanks.
Fred.
-
April 3rd, 2014, 08:39 AM
#2
Re: Create and use a wrapper class in java
Hum, not too much answer here...
-
April 10th, 2014, 12:57 AM
#3
Re: Create and use a wrapper class in java
Could you specify the reason why you require a wrapper?
-
April 10th, 2014, 06:13 AM
#4
Re: Create and use a wrapper class in java
Originally Posted by MrStiMu
Could you specify the reason why you require a wrapper?
Hello.
I have develop a native library, Java compatible.
I want a "personal" wrapper for that library so, each java application using that native library can access it via the same wrapper-class.
Thank you.
-
April 10th, 2014, 02:07 PM
#5
Re: Create and use a wrapper class in java
Originally Posted by fredvs
Hello.
I have develop a native library, Java compatible.
I want a "personal" wrapper for that library so, each java application using that native library can access it via the same wrapper-class.
Thank you.
Ok, so you would like a façade or abstraction for your native library. I assume you want to be able to easy adapt or change the native library.
The wrapper class as you've programmed it can be used statically.
Code:
public class OtherApplication {
public void test(){
TheWrapper.nativemethod1("Test");
}
}
Is this the answer you are looking for?
-
April 10th, 2014, 02:20 PM
#6
Re: Create and use a wrapper class in java
Originally Posted by MrStiMu
Ok, so you would like a façade or abstraction for your native library. I assume you want to be able to easy adapt or change the native library.
The wrapper class as you've programmed it can be used statically.
Code:
public class OtherApplication {
public void test(){
TheWrapper.nativemethod1("Test");
}
}
Is this the answer you are looking for?
Hello and many thank to answer.
It is exaxtly what i want. => a independant class that include all the "public static native xx method(x x);"
And all the other applications will use that class for calling method from that library.
So no need to declare the native method in each new application.
But how to create that "wrapper" class ?
Many thanks.
Fred
-
April 10th, 2014, 02:21 PM
#7
Re: Create and use a wrapper class in java
Glad to be of assistance.
-
April 10th, 2014, 02:23 PM
#8
Re: Create and use a wrapper class in java
=> Glad to be of assistance.
Yep, many thanks but... how to create that class ?
-
April 10th, 2014, 02:26 PM
#9
Re: Create and use a wrapper class in java
Just like you would any other class. Create a file called TheWrapper.jave and put your code in it.
Perhaps it might be interesting for you to use an editor like Eclipse: www.eclipse.org. It abstract some of the basic parts of the Java language.
-
April 10th, 2014, 04:24 PM
#10
Re: Create and use a wrapper class in java
Ok, it seems so simple....
I will try that.
Many, many thanks.
-
April 14th, 2014, 05:28 AM
#11
Re: Create and use a wrapper class in java
Hello.
I have try with this "wrapper" class :
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");}
...
}
And for the main app :
public class test {
// 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)
{
TheWrapper.nativemethod1();
TheWrapper.nativemethod2();
TheWrapper.nativemethod3();
...
}
}
OK, both compile but if i try to run test.java i get that error message :
Erreur :the main method cannot be find in TheWrapper class, define it like this :
public static void main(String[] args)
Hum, i do not understand, why must i have main method in the wrapper class and if i need it, how to use it ?
Last edited by fredvs; April 14th, 2014 at 03:50 PM.
-
April 19th, 2014, 06:37 PM
#12
Re: Create and use a wrapper class in java
Hello.
Please, why the code is not working ?
Thanks.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|