This discussion is archived
1 2 Previous Next 25 Replies Latest reply: Sep 14, 2011 11:58 AM by 888281 RSS

JNI sample problem

837475 Newbie
Currently Being Moderated
Hi, I am trying to make a sample native method to work but not able till now. I need help to understand what I am doing wrong.


This is the example I am following : http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html

The example is for linux, but I am using windows XP ( In the part of including directories I add win32 from java folder, except linux - I hope this is not the problem).

I do everything from the example good, build without errors, except the last build. It gives me this message:
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidemojava.Main.nativePrint()V
        at jnidemojava.Main.nativePrint(Native Method)
        at jnidemojava.Main.main(Main.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
and this is the code in my main method (done as in the example)


...

I am using Netbeans IDE 6.9.1

Please, every help will be appreciated. I am stuck with this samples, try about ten different samples from the net, and still have not been made one of them working ... It is important for me to make one of them working because I am new in JNI and reading something like book about it but in the book everything is done in eclipse and to continue with the reading I have to understand how to make the samples there in NetBeans IDE.

Thanks in advance.


EDIT: I was not given to extract the code. I message tells my : " Sorry, this content is not allowed " ... I will try to past it somehow...

Edited by: gotqn on Feb 27, 2011 2:35 PM

Edited by: gotqn on Feb 27, 2011 2:36 PM
  • 1. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    I have not succeedеd to add the main class but it is able to be seen in the sample link. I have searched the net for the error and understood that it is about bad linking. I have try to apply some methods for solving my problem.

    1) I have created new projects in order to have path without space

    2) I have add the path to my .so object in the Virtual Machine options

    3) I have used the method load library instead load.
    static{
    
         System.loadLibrary("libHelloWorldC");
    
       }
    This is the error message I have been given after building the project:
    run:
    java.lang.UnsatisfiedLinkError: no libHelloWorldC in java.library.path
            at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
            at java.lang.Runtime.loadLibrary0(Runtime.java:823)
            at java.lang.System.loadLibrary(System.java:1028)
            at helloworld.Main.<clinit>(Main.java:16)
    Could not find the main class: helloworld.Main.  Program will exit.
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Has anyone idea what other method I can try in order to fix the problem or what is the root of the problem itself?

    Thanks in advance.

    Edited by: gotqn on Feb 27, 2011 5:28 PM

    Edited by: gotqn on Feb 27, 2011 5:29 PM

    Edited by: gotqn on Feb 27, 2011 5:29 PM

    Edited by: gotqn on Feb 27, 2011 5:30 PM

    Edited by: gotqn on Feb 27, 2011 5:30 PM

    Edited by: gotqn on Feb 27, 2011 5:31 PM

    Edit: I am not given to show you my pathways in point 1) and 2) - why I am stopped by the system to publish them?

    Edited by: gotqn on Feb 27, 2011 5:31 PM

    Edited by: gotqn on Feb 27, 2011 7:13 PM

    Edit: Can anyone tell me what's the difference in Java Native Interface when we are creating .dll object and .so object?

    Edited by: gotqn on Feb 27, 2011 7:14 PM
  • 2. Re: JNI sample problem
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Normal cause of that error
    - The library isn't being loaded
    - Library load throws exceptions which are ignored.
    - The signature of the JNI method is wrong.

    Since you didn't post any code no more help is possible.
  • 3. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    I really want to post all of my code, but I am not able to do that because the system stops me... It says to me "Error      Sorry, this content is not allowed ".

    Please, tell my how to avoid this error and to post my code. I know that it is not right to ask people for help without posting code but for some reasons I am not able. Is there a way to post it.



    After all, all my classes are build without errors. The code is the same us the code in the sample I have followed ( and link to it is given).


    It will be grateful, if you tell me how to post my code :?
  • 4. Re: JNI sample problem
    EJP Guru
    Currently Being Moderated
    You are getting this after loading the library, when actually calling the method. So the method signature doesn't agree with what Java is expecting. This can happen when copying code if for example you change the Java class or package name after running javah. You would have to post the Java native method signature and your .h file. The system should let you do both of those.
  • 5. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    Thanks for the answer. I am not using javah because I have problem with it. I am using JNI stub program - it has been add to NetBeans IDE and I can generate .h file just with two clicks on the class.

    You were wrong I am not able to post my code. This is link with the three pictures - on main clas, on header file, on method - you are able to view them online (firstly, they are not good to be seen, but click on one of them and they will be open in separate window and been very easy to be seen even three at the same time.

    I am very sorry, but I am not able to past the code, and I am not tell what's wrong with it. Please, check my code for the error and for the reason the system do not let me publish my code.

    Here is the link:

    http://www.mypicx.com/02282011/Java_Native_Interface/
  • 6. Re: JNI sample problem
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    gotqn wrote:
    Thanks for the answer. I am not using javah because I have problem with it. I am using JNI stub program - it has been add to NetBeans IDE and I can generate .h file just with two clicks on the class.
    It doesn't matter how it runs as long as it does run and then you use the result. Using the result is not automatic. It is something that you must manually do.
    You were wrong I am not able to post my code. This is link with the three pictures - on main clas, on header file, on method - you are able to view them online (firstly, they are not good to be seen, but click on one of them and they will be open in separate window and been very easy to be seen even three at the same time.
    Start with a SMALL example.

    No one wants to see your entire application. And I for one will not look at it. Nor will I click on links.
  • 7. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    jshell, thanks for the answers, I understand that you do not want to click on links that may make to go to unsecured areas in the net ... It is a kind of insulting because I have just wanted to be helped, not to the something bad to people I do not know. Anyway, this is your right and I respect it.

    It is very useless for me that I am not able to post my code and at the same time I am not said what is wrong with it. My code is not something big, we are talking about my first Java Native Interface program. It includes only few lines.

    I really need help, so I will try to post anything it is possible.


    This is part of java class main. I am not able to past the static load method, so I have to describe it: "I am adding the path way in the project VM options filed and I use load library adding only the name of the object without its extension."
     public static void main(String[] args) {
            new Main().nativePrint();
        }
    
        private native void nativePrint();
    This is my header file, that has been generated and add into the C/C++ library project:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class helloworld_Main */
    
    #ifndef _Included_helloworld_Main
    #define _Included_helloworld_Main
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     helloworld_Main
     * Method:    nativePrint
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_helloworld_Main_nativePrint
      (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    This is the source file and the native method:
    #include <jni.h>
    #include <stdio.h>
    #include "Main.h"
    
    JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
            (JNIEnv *env, jobject obj)
    {
    
        printf("\nHello World from C\n");
    
    }
    Edited by: gotqn on Mar 1, 2011 2:21 AM

    Edited by: gotqn on Mar 1, 2011 2:24 AM

    Edited by: gotqn on Mar 1, 2011 2:25 AM
  • 8. Re: JNI sample problem
    EJP Guru
    Currently Being Moderated
    no libHelloWorldC in java.library.path
    That message is pretty clear. Did you build a shared library? Is that its name? and where did you put it? Try '.' for a start.
  • 9. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    I try two different ways. First, I have built shared object, extension .so and second dll library - extension .dll. I have wanted to understand whats the difference and what's better to be used.

    I try two different ways to add the path to the dll and so too.

    I add the path in the java project -> run -> VM options and use System load library and second using system load.
    static{
        System.load("C:/NetBeansProjects/HelloWorldC/dist/libHelloWorldC.so");
         //System.load("C:/NetBeansProjects/HelloWorldC/dist/libHelloWorldC.dll");
         //System.loadLibrary("libHelloWorldC");
    
       }
    I put the library in this path (shown in the code). I am pretty sure because I do what is said in the sample and I check it opening the folder.

    Edited by: gotqn on Mar 1, 2011 2:32 AM


    EDIT: I can swear that earlier I was not able to post code that I have posted in my previous post. I really can get this...

    Edited by: gotqn on Mar 1, 2011 2:33 AM
  • 10. Re: JNI sample problem
    EJP Guru
    Currently Being Moderated
    I have wanted to understand whats the difference
    The difference is that .so files are for Linux, Unix, Solaris, AIX, etc, and .dll files are for Windows.
    and what's better to be used.
    That's meaningless. There's no 'better' about it. You don't get the choice. The platform dictates which one you have to use. The purpose of System.loadLibrary is to shield you from the issues of where it actually is and whether it is a .so or a .dll. So use that. And install the shared library in '.' as I said above.
  • 11. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    Thanks for the answers. What do you mean to install the library in "." - you want to install it in folder called "." , not in the "dist" folder?
  • 12. Re: JNI sample problem
    EJP Guru
    Currently Being Moderated
    '.' is the current directory. Where you are when you execute the 'java' command.
  • 13. Re: JNI sample problem
    837475 Newbie
    Currently Being Moderated
    It makes no differences. Maybe I do something wrong?

    This is what I have done:
    1. Open C/C++ project , changed the linker output as ./libHelloWorldC.dll .
    2. Build and Clean.
    3. Have the dll in the project directory, not in the dist folder of the project.
    4. Changed the java project VM option to -Djava.library.path=C:\NetBeansProjects\HelloWorldC
    5. Run... the same erorr
  • 14. Re: JNI sample problem
    EJP Guru
    Currently Being Moderated
    Where did you get the 'lib' part of the .dll name from? That's for .so files. Remove it if you are creating a .dll file.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points