UnsatisfiedLinkError for a basic native method











up vote
1
down vote

favorite












I have those two super basic methods in Java like so:



package com.lisek;

class HelloWorldJNI {

static {
System.loadLibrary("native");
}

public static void main(String args) {
HelloWorldJNI helloWorldJNI = new HelloWorldJNI();
helloWorldJNI.sayHello();
helloWorldJNI.sayHello2();
}

// Declare a native method sayHello() that receives no arguments and returns void
private native void sayHello();
private native void sayHello2();
}


I am generating JNI files like so javac -h . HelloWorldJNI.java which results in such com_lisek_HelloWorldJNI.h file:



/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_lisek_HelloWorldJNI */

#ifndef _Included_com_lisek_HelloWorldJNI
#define _Included_com_lisek_HelloWorldJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_lisek_HelloWorldJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello
(JNIEnv *, jobject);

/*
* Class: com_lisek_HelloWorldJNI
* Method: sayHello2
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


And I am doing implementation of those in the same exact manner in the com_lisek_HelloWorldJNI.cpp file like so:



#include "com_lisek_HelloWorldJNI.h"
#include <stdio.h>
#include <iostream>
#include <jni.h>

JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
std::cout << "Hello from C++ !!" << std::endl;
}

JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2 (JNIEnv* env, jobject thisObject) {
std::cout << "Hello from C++ !!" << std::endl;
}


I am compiling the code with g++ -c -I"C:Program FilesJavajdk1.8.0_181include" -I"C:Program FilesJavajdk1.8.0_181includewin32" com_lisek_HelloWorldJNI.cpp -o com_lisek_HelloWorldJNI.o and generating .dll with g++ -shared -o native.dll com_lisek_HelloWorldJNI.o -Wl,--add-stdcall-alias. All looks very well to me and I can not spot any flaw of this code but nevertheless in Java while running the code I am getting exception only on the second method:



Hello from C++ !!
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.lisek.HelloWorldJNI.sayHello2()V
at com.lisek.HelloWorldJNI.sayHello2(Native Method)
at com.lisek.HelloWorldJNI.main(HelloWorldJNI.java:12)


It means to me that the .dll is loaded properly as if I do two calls of the sayHello() I will get the Hello twice. Seriously, what is going on?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have those two super basic methods in Java like so:



    package com.lisek;

    class HelloWorldJNI {

    static {
    System.loadLibrary("native");
    }

    public static void main(String args) {
    HelloWorldJNI helloWorldJNI = new HelloWorldJNI();
    helloWorldJNI.sayHello();
    helloWorldJNI.sayHello2();
    }

    // Declare a native method sayHello() that receives no arguments and returns void
    private native void sayHello();
    private native void sayHello2();
    }


    I am generating JNI files like so javac -h . HelloWorldJNI.java which results in such com_lisek_HelloWorldJNI.h file:



    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_lisek_HelloWorldJNI */

    #ifndef _Included_com_lisek_HelloWorldJNI
    #define _Included_com_lisek_HelloWorldJNI
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class: com_lisek_HelloWorldJNI
    * Method: sayHello
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello
    (JNIEnv *, jobject);

    /*
    * Class: com_lisek_HelloWorldJNI
    * Method: sayHello2
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2
    (JNIEnv *, jobject);

    #ifdef __cplusplus
    }
    #endif
    #endif


    And I am doing implementation of those in the same exact manner in the com_lisek_HelloWorldJNI.cpp file like so:



    #include "com_lisek_HelloWorldJNI.h"
    #include <stdio.h>
    #include <iostream>
    #include <jni.h>

    JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
    std::cout << "Hello from C++ !!" << std::endl;
    }

    JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2 (JNIEnv* env, jobject thisObject) {
    std::cout << "Hello from C++ !!" << std::endl;
    }


    I am compiling the code with g++ -c -I"C:Program FilesJavajdk1.8.0_181include" -I"C:Program FilesJavajdk1.8.0_181includewin32" com_lisek_HelloWorldJNI.cpp -o com_lisek_HelloWorldJNI.o and generating .dll with g++ -shared -o native.dll com_lisek_HelloWorldJNI.o -Wl,--add-stdcall-alias. All looks very well to me and I can not spot any flaw of this code but nevertheless in Java while running the code I am getting exception only on the second method:



    Hello from C++ !!
    Exception in thread "main" java.lang.UnsatisfiedLinkError: com.lisek.HelloWorldJNI.sayHello2()V
    at com.lisek.HelloWorldJNI.sayHello2(Native Method)
    at com.lisek.HelloWorldJNI.main(HelloWorldJNI.java:12)


    It means to me that the .dll is loaded properly as if I do two calls of the sayHello() I will get the Hello twice. Seriously, what is going on?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have those two super basic methods in Java like so:



      package com.lisek;

      class HelloWorldJNI {

      static {
      System.loadLibrary("native");
      }

      public static void main(String args) {
      HelloWorldJNI helloWorldJNI = new HelloWorldJNI();
      helloWorldJNI.sayHello();
      helloWorldJNI.sayHello2();
      }

      // Declare a native method sayHello() that receives no arguments and returns void
      private native void sayHello();
      private native void sayHello2();
      }


      I am generating JNI files like so javac -h . HelloWorldJNI.java which results in such com_lisek_HelloWorldJNI.h file:



      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class com_lisek_HelloWorldJNI */

      #ifndef _Included_com_lisek_HelloWorldJNI
      #define _Included_com_lisek_HelloWorldJNI
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
      * Class: com_lisek_HelloWorldJNI
      * Method: sayHello
      * Signature: ()V
      */
      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello
      (JNIEnv *, jobject);

      /*
      * Class: com_lisek_HelloWorldJNI
      * Method: sayHello2
      * Signature: ()V
      */
      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2
      (JNIEnv *, jobject);

      #ifdef __cplusplus
      }
      #endif
      #endif


      And I am doing implementation of those in the same exact manner in the com_lisek_HelloWorldJNI.cpp file like so:



      #include "com_lisek_HelloWorldJNI.h"
      #include <stdio.h>
      #include <iostream>
      #include <jni.h>

      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
      std::cout << "Hello from C++ !!" << std::endl;
      }

      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2 (JNIEnv* env, jobject thisObject) {
      std::cout << "Hello from C++ !!" << std::endl;
      }


      I am compiling the code with g++ -c -I"C:Program FilesJavajdk1.8.0_181include" -I"C:Program FilesJavajdk1.8.0_181includewin32" com_lisek_HelloWorldJNI.cpp -o com_lisek_HelloWorldJNI.o and generating .dll with g++ -shared -o native.dll com_lisek_HelloWorldJNI.o -Wl,--add-stdcall-alias. All looks very well to me and I can not spot any flaw of this code but nevertheless in Java while running the code I am getting exception only on the second method:



      Hello from C++ !!
      Exception in thread "main" java.lang.UnsatisfiedLinkError: com.lisek.HelloWorldJNI.sayHello2()V
      at com.lisek.HelloWorldJNI.sayHello2(Native Method)
      at com.lisek.HelloWorldJNI.main(HelloWorldJNI.java:12)


      It means to me that the .dll is loaded properly as if I do two calls of the sayHello() I will get the Hello twice. Seriously, what is going on?










      share|improve this question













      I have those two super basic methods in Java like so:



      package com.lisek;

      class HelloWorldJNI {

      static {
      System.loadLibrary("native");
      }

      public static void main(String args) {
      HelloWorldJNI helloWorldJNI = new HelloWorldJNI();
      helloWorldJNI.sayHello();
      helloWorldJNI.sayHello2();
      }

      // Declare a native method sayHello() that receives no arguments and returns void
      private native void sayHello();
      private native void sayHello2();
      }


      I am generating JNI files like so javac -h . HelloWorldJNI.java which results in such com_lisek_HelloWorldJNI.h file:



      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class com_lisek_HelloWorldJNI */

      #ifndef _Included_com_lisek_HelloWorldJNI
      #define _Included_com_lisek_HelloWorldJNI
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
      * Class: com_lisek_HelloWorldJNI
      * Method: sayHello
      * Signature: ()V
      */
      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello
      (JNIEnv *, jobject);

      /*
      * Class: com_lisek_HelloWorldJNI
      * Method: sayHello2
      * Signature: ()V
      */
      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2
      (JNIEnv *, jobject);

      #ifdef __cplusplus
      }
      #endif
      #endif


      And I am doing implementation of those in the same exact manner in the com_lisek_HelloWorldJNI.cpp file like so:



      #include "com_lisek_HelloWorldJNI.h"
      #include <stdio.h>
      #include <iostream>
      #include <jni.h>

      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
      std::cout << "Hello from C++ !!" << std::endl;
      }

      JNIEXPORT void JNICALL Java_com_lisek_HelloWorldJNI_sayHello2 (JNIEnv* env, jobject thisObject) {
      std::cout << "Hello from C++ !!" << std::endl;
      }


      I am compiling the code with g++ -c -I"C:Program FilesJavajdk1.8.0_181include" -I"C:Program FilesJavajdk1.8.0_181includewin32" com_lisek_HelloWorldJNI.cpp -o com_lisek_HelloWorldJNI.o and generating .dll with g++ -shared -o native.dll com_lisek_HelloWorldJNI.o -Wl,--add-stdcall-alias. All looks very well to me and I can not spot any flaw of this code but nevertheless in Java while running the code I am getting exception only on the second method:



      Hello from C++ !!
      Exception in thread "main" java.lang.UnsatisfiedLinkError: com.lisek.HelloWorldJNI.sayHello2()V
      at com.lisek.HelloWorldJNI.sayHello2(Native Method)
      at com.lisek.HelloWorldJNI.main(HelloWorldJNI.java:12)


      It means to me that the .dll is loaded properly as if I do two calls of the sayHello() I will get the Hello twice. Seriously, what is going on?







      java c++ jni






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 11:20









      Lisek

      135217




      135217
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Remember to compile and run your code with -Djava.library.path flag set like so:



          java -cp . -Djava.library.path="D:\Workspace\JNI\Callback\src\com\lisek" com.lisek.HelloWorldJNI



          But it still dazzles me that it was able to run the first sayHello() but was not able to run sayHello2(). Can't think of any reasonable idea why this happens.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206725%2funsatisfiedlinkerror-for-a-basic-native-method%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Remember to compile and run your code with -Djava.library.path flag set like so:



            java -cp . -Djava.library.path="D:\Workspace\JNI\Callback\src\com\lisek" com.lisek.HelloWorldJNI



            But it still dazzles me that it was able to run the first sayHello() but was not able to run sayHello2(). Can't think of any reasonable idea why this happens.






            share|improve this answer

























              up vote
              0
              down vote













              Remember to compile and run your code with -Djava.library.path flag set like so:



              java -cp . -Djava.library.path="D:\Workspace\JNI\Callback\src\com\lisek" com.lisek.HelloWorldJNI



              But it still dazzles me that it was able to run the first sayHello() but was not able to run sayHello2(). Can't think of any reasonable idea why this happens.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Remember to compile and run your code with -Djava.library.path flag set like so:



                java -cp . -Djava.library.path="D:\Workspace\JNI\Callback\src\com\lisek" com.lisek.HelloWorldJNI



                But it still dazzles me that it was able to run the first sayHello() but was not able to run sayHello2(). Can't think of any reasonable idea why this happens.






                share|improve this answer












                Remember to compile and run your code with -Djava.library.path flag set like so:



                java -cp . -Djava.library.path="D:\Workspace\JNI\Callback\src\com\lisek" com.lisek.HelloWorldJNI



                But it still dazzles me that it was able to run the first sayHello() but was not able to run sayHello2(). Can't think of any reasonable idea why this happens.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 12:22









                Lisek

                135217




                135217






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206725%2funsatisfiedlinkerror-for-a-basic-native-method%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)