Native methods inside C++ class

Hey all,
Tried posting this already but it didn't appear in the forums. So apologies if this appears twice on same forum :o)
Want to wrap my native methods inside a C++ class but get UnsatisfiedLinkerError when I do. Is it possible to do this? Do I have to change some method signatures or what?
I want something like :
public class A
native void doSomething();
class B
public:
JNIEXPORT void JNI Java_A_doSomething() { ... } /* or whatever */
It works ok outside of a C++ class but I'd prefer to contain my methods inside a class. Any help?
Cheers,
Conor

Java needs to find your functions in the DLL or SO. But the names are "mangled" if you declare the function inside a class - even declaring the function "static" does not help. For instance, using the Microsoft C++ compiler:
class B
public:
static JNIEXPORT void JNI Java_A_doSomething() { ... }
};the name is mangled to ?Java_A_doSomething@B@@SAXXZ
But Java tries to locate the entry _Java_A_doSomething@0 in the DLL.
You can write the global functions as:
JNIEXPORT void JNI Java_A_doSomething() {
    B::doSomething();
}and implement your functions in the class B. The advantage of this approach is that you can write all bookkeeping work of converting Java data to C++ data in the global function, and leave the real work to the function declared in the C++ class.

Similar Messages

  • What is the purpose of Static methods inside a class?

    Hi,
    What is the purpose of Static methods inside a class?
    I want the answers apart from "A static method does not require instance of class(to access) and it can directly be accessed by the class name itself"
    My question is what is the exact purpose of a static method ?
    Unlike attributes, a separate copy of instance attributes will be created for each instance of a class where as only one copy of static attributes will be created for all instances.
    Will a separate copy of instance method be created for each instance of a class and only one copy of static methods be create?
    Points will be rewarded for all helpful answers.

    Hi Sharma,
    Static methods is used to access statics attributes of a class. We use static attributes when we want to share the same attribute with all instances of a class, in this case if you chage this attribute through the instance A this change will change will be reflected in instance B, C........etc.
    I think that your question is correct -> a separate copy of instance method will be created for each instance of a class and only one copy of static methods be create ?
    "A static method does not require instance of class(to access) and it can directly be accessed by the class name itself"
    Static Method: call method class=>method.
    Instance Method: call method instance->method.
    Take a look at this wiki pages.
    [https://wiki.sdn.sap.com/wiki/x/o5k]
    [https://wiki.sdn.sap.com/wiki/x/ZtM]
    Best regards.
    Marcelo Ramos

  • Implement method inside abstract class?

    hello everyone:
    I have a question regarding implementation of method inside a abstract class. The abstract class has a static method to swamp two numbers.
    The problem ask if there is any output from the program; if no output explain why?
    This is a quiz question I took from a java class. I tried the best to recollect the code sample from my memory.
    if the code segment doesn't make sense, could you list several cases that meet the purpose of the question. I appreciate your help!
    code sample
    public abstract class SwampNumber
       int a = 4;
       int b = 2;
       System.out.println(a);
       System.out.println(b);
       swamp(a, b);
       public static void swamp(int a, int b)
         int temp = a;
             a = b;
             b = a;
         System.out.println(a);
         System.out.println(b);

    It won't compile.
    You can't instantiate an abstract class before you know anything.
    //somewhere in main
    SwampNumber myNum = new SwampNumber();
    //Syntax ErrorQuote DrClap
    This error commonly occurs when you have code that >>is not inside a method.
    The only statements that can appear outside methods >>in a Java class are >>declarations.Message was edited by:
    lethalwire

  • Native method in local class

    Hi there,
    this is a repost of my question but now in the right forum.
    I have a problem with a native method of a local class that hides a member class:
    public class SomeClass
         class MemberClass
              public native void bla();
              public void out()
                   System.out.println("A");
         public void foo()
              class MemberClass
                   public native void bla();
                   public void out()
                        System.out.println("B");
              MemberClass myClass = new MemberClass();
              myClass.out();
         public static void main(String[] args)
              SomeClass someClass = new SomeClass();
              someClass.foo();
    }The class MemberClass exist in two different ways. Every class has it's own out() method. The local class just hides the member class. Now I want to implement the native methods bla() of the two classes.
    If I call
    javac SomeClass.hI get
    SomeClass.class
    SomeClass$MemberClass.class
    SomeClass$1MemberClass.classwhich makes sense to me. We have one class file for the member class and one for the local class.
    If I use
    javah SomeClassthen I get
    JNIEXPORT void JNICALL Java_SomeClass_00024MemberClass_bla(JNIEnv *, jobject);in the header file for the member class. No header file for the local class was created.
    Therefore I call
    javah SomeClass$1MemberClassand I get another header file which is for the local class. But this header file declares the same native method:
    JNIEXPORT void JNICALL Java_SomeClass_00024MemberClass_bla(JNIEnv *, jobject);This is ambiguous... If I would implement the native method, then I couldn't even say for which one of the native methods it will be used. Maybe for both.. which wouldn't make any sense too.
    What is wrong here?
    Thank you for your thoughts :-)

    Interesting...From the JLS under "fully qualified name"
    [http://java.sun.com/docs/books/jls/third_edition/html/names.html#71701]
    "Every package, top level class, top level interface, and primitive type has a fully qualified name."
    From the javah docs
    javah [ options ] fully-qualified-classname
    And note this under the JNI spec for "FindClass"
    [http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp16027]
    "The name argument is a fully-qualified class name or an array type signature "
    Does that mean that you can't use JNI to access a local class? Seems like I have seen posts that said that was possible.

  • Native method in object class........

    I would like to know that which method of object class is a native method..........

    flounder wrote:
    The natives are getting restless.
    What they do with him when he arrives, I don't know.This reminds me of one of my favorite entries in Ambrose Bierce's [Devil's Dictionary|http://en.wikipedia.org/wiki/Devil%27s_dictionary]
    h3. Aborigines
    Persons of little worth found cumbering the soil of a newly discovered country. They soon cease to cumber; they fertilize.

  • Error When Deleting a Method Inside a Class

    I developed a custom class that contained a private method. I removed this method because it was no longer needed. When moving the changes from the development to test client, it failed on import. For some reason, there was still some dependency on the method that was deleted.
    I went back to transaction SE24 and regenerated all the sections. When this was done, it regenerated the class definition and the various sections. I moved a transport that contained these changes and it still failed. The import log has a reference to an include that is failing, but I still see no mention of the method that was deleted.
    To continue going forward, I added the method back so that it resolved syntax error during import.
    I am suspecting that there is some issue with ABAP support pack being used on the development system. Have other people experienced the same issue. I don't have the habit of deleting that many methods once it is created, but would like to understand the above problem and see what should be the corrective action.
    Feedback/comments would be appreciated.
    Regards,
    Vince Castello

    As mentioned in my original post, I did re-generate all the sections using SE24, but the import still failed where the dependency to deleted method still remained.
    I am planning to create a customer message and will post results when they become available.
    Regards,
    Vince Castello

  • FATAL ERROR in native method

    Dear all,
    I am working on a MacOSX. I am having trouble in using a Dynamic Library generated from C files and accessed from a java project using JNI.
    The loading of the library work well bur when i try to access a method I have the following error:
    FATAL ERROR in native method: JNI received a class argument that is not a class
         at cytosolve.sbmlSolver.SBMLSolver.initSolver(Native Method)
         at cytosolve.sbmlSolver.SBMLSolver.<init>(SBMLSolver.java:87)
         at cytosolve.sbmlSolver.cytosolve.main(cytosolve.java:29)where the Native method inside the C file is the following:
    JNIEXPORT jint JNICALL
    Java_cytosolve_sbmlSolver_SBMLSolver_initSolver(JNIEnv *env, jobject obj, jstring model_filename, jdouble totalTime, jint numSteps)and it is called from the Java file as:
    int successCode = initSolver(model_filename, totalTime, numSteps);Do you have any idea on where is the problem and how to fix it?
    Thank you,
    Eva.

    Everything seems to be right. Maybe you use JNI functions inproperly?

  • Calling native methods in a package

    Hi!
    We have a problem when calling native methods in a class which is in a package. As you can see from the code below we have a class making an instance of a class<testBando> which makes a instance of another class<Bando> which contains the native methods. When running the program we get the error:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: initialize
    at test2.Bando.initialize(Native Method)
    at test.testBando.<init>(testBando.java:10)
    at Main.main(Main.java:5)
    When we take the class that calls the native methods out of any package every thing works fine! Can you not call native methods inside a package or what are we doing wrong?
    // testBando.java
    package test;
    import test2.*;
    public class testBando {
    Bando b;
    public testBando() {
         b = new Bando();
         b.initialize();
    // Bando.java
    package test2;
    public class Bando {
    public native void initialize();
    static {
         System.loadLibrary("bando");
    // Main.java
    import test.*;
    public class Main {
    public static void main(String[] args) {
         new testBando();

    I suspect that your problem is that you have not
    regenerated the signature when you placed the native
    method's object in a package. Or same result but different sourc:
    -package name was changed and it wasn't regen'd
    -javah was run incorrectly (which gens the signatures with no package name.)

  • Problems calling a method in another class

    I have the following method in a class called recordCalls -
    public static void objectCreated(String type, String name)
            System.out.println("An object of type " + type + " called " + name + " has been created.");
    //       addObjectToPanel(type, name);  
        }I am attempting to call addObjectToPanel(type, name) which is a method inside a class called test.
    I do not want to create an instance of test an call it like test.addObjectToPanel(type, name)
    Is there any other way of doing this.
    Thanks.

    You either have to make the method static, and call
    test.addObjectToPanel or you have to create an
    instance of test and invoke the method on that
    instance.
    I don't know what that class is supposed to do, so I
    don't know which is more appropriate.
    You should name your classes starting with capital
    letters, and Test is a very undescriptive (and hence
    bad) name for that class.I will be chaning the names of everything when the class works.
    Test contains the UI for my program.
    When I run test then my UI is runing, once I run addObjectToPanel from the record calls class it should put images into my UI,
    the problem is that each time the method is run it open up a different UI and adds an image to it instead of just adding the images to the window which is already open.

  • OnEnterFrame inside a class

    I don't have a problem as much as just a lack of
    understanding:
    Question: Will I run into any problems using onEnterFrame
    inside a class? I know that onEnterFrame events can overwrite each
    other if they are written dynamically, but I'm not sure how this
    effects classes...
    Also, onEnterFrame is an event of the movie clip class right?
    ...does that even matter...? What happens if I declare an
    onEnterFrame event inside a method inside my class: will it even
    work? will it effect the movie clip from which the class.method is
    being called?
    Sigh... Hopefully you get the idea of what I'm having trouble
    grasping. Please explain any concepts you think I appear to be
    missing. I'm new to classes - thanks for your help.

    >>Question: Will I run into any problems using
    onEnterFrame inside a class?
    No.
    >>Also, onEnterFrame is an event of the movie clip
    class right? ...does that even matter...?
    No. Here is a brief explanation how it works.
    First of all, a class is a custom object. Don't think of a
    class as a bunch of code, it's a custom object with its own
    properties and methods. So when you say: "What happens if I declare
    an onEnterFrame event inside a method inside my class: will it even
    work?" you are thinking: "there is no timeline". Yes, there is.
    When you declare:
    private var my_mc:MovieClip;
    you are saving the functionality of the MovieClip Class in
    my_mc. Therefore, my_mc will have a timeline and all other
    properties and methods of the MovieClip Class. (By comparison:
    usually you don't need all the functionality of the MovieClip
    Class, so AS3 offers us the Sprite Class which is a basic
    implementation of a movieclip and that saves overhead).
    In a method you can use:
    private function mover(target:MovieClip):Void{
    target.onEnterFrame=function():Void{
    this._x+=2;
    passing the my_mc to function mover as a parameter. If at
    some time you want to stop the onEnterFrame you delete it just as
    you would using it in code on the timeline.
    Again. The most important thing to understand is that a class
    is not a bunch of code, it's a (custom) object with its own
    properties and methods.

  • Methods in a class

    When you add more methods to a class does it increase the amount of memory the Object uses idle, or does it increase the creation time of an object?
    I'm talking around 6000 lines of code, and i was just wondering if it would be better to add all related method inside the class or to make a static class that takes one of the objects as an argument and performs the actions on it.
    Thanks.

    Peter__Lawrey wrote:
    Ok, the class is stored as one file and loading the class causes it to be unpacked (if required) and validated.
    However, if you have a corrupt method, (and I have written a few with ASM) that corruption won't be detected until you call the method. e.g. illegal instructions.
    The points is some loading occurs when the class is first used, but there is additional work which occurs when the method is called for the first time. I suspect the later is more significant for a larger method.Well, but that's not really performance-relevant. What you mean sounds to me like a simple run-time failure during the actual execution of the method. The statements were loaded but not executed, hence the fault remains unnoticed. It doesn't sound to me like there's any loading or verification work prior to invoking a method the first time (and it also wouldn't matter with regards to the overall amount of methods in a class), but then, I never implemented a JVM.

  • Calling native method

    Guys please help me with following error.
    I am running a simple JNI program,after creating and loading the dll i am getting following error.......
    Exception in thread "main" java.lang.UnsatisfiedLinkError: print
    "print" is the native method in java class Hello.java
    Hello.h has method declaration as :
    JNIEXPORT void JNICALL Java_Hello_print
    (JNIEnv *, jobject);
    and C++ file has method defined as :
    JNIEXPORT void JNICALL
    Java_Hello_print(JNIEnv *env, jobject obj)
    printf("Hello World!\n");
    return;
    please help me out...it has been 3 days banging against JNI ...

    Guys please help me with following error.
    I am running a simple JNI program,after creating and
    loading the dll i am getting following error.......
    Exception in thread "main"
    java.lang.UnsatisfiedLinkError: print
    "print" is the native method in java class
    Hello.java
    Hello.h has method declaration as :
    JNIEXPORT void JNICALL Java_Hello_print
    (JNIEnv *, jobject);
    and C++ file has method defined as :
    JNIEXPORT void JNICALL
    Java_Hello_print(JNIEnv *env, jobject obj)
    printf("Hello World!\n");
    return;
    please help me out...it has been 3 days banging
    against JNI ...If your Java code is in a package, then your native header file declaration is wrong. The javah tool expects the fully qualified class name. So, if your Hello class is in package com.foo.hello, you should generate the header file like this:
    javah com.foo.hello.HelloThen the native header declaration will look like this:
    JNIEXPORT void JNICALL Java_com_foo_hello_Hello_print
    (JNIEnv *, jobject);

  • Download code of all the methods in a class to desktop

    Hi,
    I want to download code of all the methods in a class to desktop.
    Its like user will enter Class name on selection screen, then program should download the code of all the methods inside that class on to desktop as a text file.
    How to do it.
    Points will be rewarded!!
    Regards

    You can use FM SEO_CLASS_GET_METHOD_INCLUDES
    to determine the names of all methods implemented in this class with the according names of the includes.
    Than you can loop through the table returned by this FM and get the coding of all methods using READ REPORT statement, whereas you must pass the name of the include of the according method, when executing READ REPORT...
    Reward points, in case reply meets your expectation...
    --MIKE

  • What is native method?

    hi.. i have the following error msg on the java wireless toolkit..
    "ERROR: native methods should not appear
    Error preverifying class com.zucotto.io.serialcomm.COMInputStream
    Build failed"
    can anyone help?
    thanks

    You can't "un-native" a native method. The class library that you are using relies upon native code. It can not be used on the target platform. You would have to ask the vendor of that class library to update it to work on your target platform without the native code.
    God bless,
    -Toby Reyelts

  • Error calling native methods

    HI
    I declared a static native method in a class and using the System.loadLibrary() method i load the dll.
    Now if i want to call the method from another class using instance of that class where i declare the native method, it shows an error
    java.lang.SpecifiedPathLink error. I have put the dll outside the package in the same directory.
    can anyone help me regarding this problem

    it shows this exception
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no PerfMonitor in jav
    a.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)
    at java.lang.Runtime.loadLibrary0(Runtime.java:788)
    at java.lang.System.loadLibrary(System.java:834)
    at C3.CPUMemoryUsage.<clinit>(CPUMemoryUsage.java:6)
    at C3.Node.main(Node.java:18)

Maybe you are looking for