CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2014
    Posts
    8

    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.

  2. #2
    Join Date
    Apr 2014
    Posts
    8

    Re: Create and use a wrapper class in java

    Hum, not too much answer here...

  3. #3
    Join Date
    Apr 2014
    Posts
    4

    Re: Create and use a wrapper class in java

    Could you specify the reason why you require a wrapper?

  4. #4
    Join Date
    Apr 2014
    Posts
    8

    Re: Create and use a wrapper class in java

    Quote Originally Posted by MrStiMu View Post
    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.

  5. #5
    Join Date
    Apr 2014
    Posts
    4

    Re: Create and use a wrapper class in java

    Quote Originally Posted by fredvs View Post
    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?

  6. #6
    Join Date
    Apr 2014
    Posts
    8

    Re: Create and use a wrapper class in java

    Quote Originally Posted by MrStiMu View Post
    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

  7. #7
    Join Date
    Apr 2014
    Posts
    4

    Re: Create and use a wrapper class in java

    Glad to be of assistance.

  8. #8
    Join Date
    Apr 2014
    Posts
    8

    Re: Create and use a wrapper class in java

    => Glad to be of assistance.

    Yep, many thanks but... how to create that class ?

  9. #9
    Join Date
    Apr 2014
    Posts
    4

    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.

  10. #10
    Join Date
    Apr 2014
    Posts
    8

    Re: Create and use a wrapper class in java

    Ok, it seems so simple....
    I will try that.
    Many, many thanks.

  11. #11
    Join Date
    Apr 2014
    Posts
    8

    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.

  12. #12
    Join Date
    Apr 2014
    Posts
    8

    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
  •  





Click Here to Expand Forum to Full Width

Featured