8 Replies Latest reply on Feb 17, 2011 7:52 PM by jschellSomeoneStoleMyAlias

    How to call non static method?

    838507
      Hi,

      How to call non static java method from C++ using JNI?

      I tried to use env->callIntMethod(jobject jobj, method_id mid, int i), but I just don't understand how can I get "jobject" instance. Do I need to call class constructor? how?

      Edited by: user3516155 on Feb 16, 2011 5:26 AM
        • 1. Re: How to call non static method?
          802316
          To call a non-static method you have to have an instance to call on.
          Why are you calling the method and what do you expect it to do?
          Once you know that you will know what object you need to be calling on and whether it needs to be created first.
          • 2. Re: How to call non static method?
            EJP
            And if it does need to be created first there is almost certainly something wrong with your design, unless you plan on doing something else with it afterwards, which you should already know about.
            • 3. Re: How to call non static method?
              838507
              Actually I need to build one java application which should be called from C++ app using JNI. This C++ application will communicate with external component on USB port and will redirect call to Java application using JNI. In return java will process the request and will call C++ application which will then communicate with the OS or mozilla browser. Here I am able to call static method from java which will make all other methods static which are called from static method. So would like to call non static instance method. Please let me know any other alternate solution or the how to resolve the non static method call.
              • 4. Re: How to call non static method?
                EJP
                So would like to call non static instance method.
                So create or acquire the object and invoke the method on it.
                Please let me know any other alternate solution or the how to resolve the non static method call.
                There is no other solution. You need an object to call a non-static method. Period.
                • 5. Re: How to call non static method?
                  838507
                  Thanks for suggestion.

                  Could you please suggest me how to aquire object instance, I mean do I need to call class constructor to build the same or is there any other standard method for the same to get the jobject for that class?

                  If I continue with the static method call from C++ which will make us to define all other java methods as static as well. Will this approach make any problem I mean can you see any issues in the same?
                  • 6. Re: How to call non static method?
                    EJP
                    Could you please suggest me how to aquire object instance, I mean do I need to call class constructor to build the same or is there any other standard method for the same to get the jobject for that class?
                    How would you do it in Java? Whatever your answer is to that, the same answer applies to JNI code.
                    • 7. Re: How to call non static method?
                      838507
                      ok.

                      If I continue with the static method call from C++ which will make us to define all other java methods as static as well because each and every method is routed through the static method. Will this approach make any problem I mean can you see any issues in the same?
                      • 8. Re: How to call non static method?
                        jschellSomeoneStoleMyAlias
                        can you see any issues in the same?
                        Why use java at all? Just write it all in C++.

                        Other than that given that you have existing code in java that you want to use then you create a business interface that produces exactly the functionality that you want to use in C++. For example, make the java code static methods (the java code creates the object that you are referring to, makes the call, and returns the result.) Then your C++ code uses that.