1 2 Previous Next 25 Replies Latest reply: Sep 14, 2011 1:58 PM by 888281 Go to original post RSS
      • 15. Re: JNI sample problem
        837475
        As I know, I define the type of the file that is created when I clean and build the c++/c project using the project properties.

        First, on C compiler's properties I add include and win32 directories.
        Then, on C compiler's properties I add in additional option field -m32 (for using 32 bit system) and -shared for creating a shared object
        Last, in Linker, output field i add the path when the file will be created and its extension:
        - if i said libName.so the result is shared object
        -if i write .dll, the result is dll library

        I use the same path written there as my VM options path in the Java project.
        • 16. Re: JNI sample problem
          jschellSomeoneStoleMyAlias
          System.load("C:/NetBeansProjects/HelloWorldC/dist/libHelloWorldC.so");
          Need to slow down a bit on this discussion.

          There are only two possibilities for the load() method.
          1. It loaded a library.
          2. It didn't load a library and threw an exception.

          The fact that the above name and path is odd doesn't mean that it has anything to do with the original problem in this thread. All that matters is that it is a library and that it did load.

          If it did in fact load a library and it still doesn't find the method then one is left with
          1. The method is not in that file (again if if loaded it is a library.)
          2. The method signature is different.
          • 17. Re: JNI sample problem
            jschellSomeoneStoleMyAlias
            gotqn wrote:
            4. Changed the java project VM option to -Djava.library.path=C:\NetBeansProjects\HelloWorldC
            Meaningless since you are using load()
            • 18. Re: JNI sample problem
              EJP
              - if i said libName.so the result is shared object
              -if i write .dll, the result is dll library
              No. In both cases the result is a linked file of the type that the linker produces. Just changing the extension doesn't change the result type.

              As you are running on Windows the file should be named .dll and it should not start with 'lib'. You should locate it in '.' and you should load it with System.loadLibrary(), providing just its name without the .dll extension and without specifying any directories.

              I've said all this before. You could always try it, instead of just reiterating what you have done wrong, and dreaming up specious reasons for it.
              • 19. Re: JNI sample problem
                837475
                EJP, thanks for the advice, but it did not work for me.

                I have deleted my projects and started the example again doing everything as you have told me. I have located the file in '.' and load it with System.loadLibrary() wtihout the .dll extension and without specifying any directories.

                The message error is the same:
                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 as this is the code:
                /*
                 * To change this template, choose Tools | Templates
                 * and open the template in the editor.
                 */
                
                package jnidemojava;
                
                /**
                 *
                 * @author Joro
                 */
                public class Main {
                
                        static {
                        System.loadLibrary("JNIDemoCdl");
                       }
                
                
                    public static void main(String[] args) {
                       new Main().nativePrint();
                    }
                
                    private native void nativePrint();
                
                    
                }
                this row seems to be the reason for the error
                 new Main().nativePrint();
                I have started to doubt that there is something wrong with my method, not the load of the library. Can you explain me what's the problem with signature of the method that the other man has told me about?
                • 20. Re: JNI sample problem
                  EJP
                  So you are now loading the library successfully. That's progress. You now have an issue with the actual call. Provided the correct version of the .dll is being loaded and provided the C source code looks as above and provided the compile and link options are correct and provided the Java source code looks as above it should work. I would suspect the compile/link options at the moment. Can you produce a map file and post the line that lists this entry point?
                  • 21. Re: JNI sample problem
                    837475
                    Thanks for the paying attention, EGP.

                    How can I produce map file?

                    I want to know something more. I have been told that the problem may be not correct build of the .dll file. So, I doubt that I do not set the correct adjustments in the c/c++ dynamic library project before the build.

                    I add two directories to the c compiler - the include one and the win32 one from the java developer kit. I have four versions of it - three versions 6.0 and one version 6.0_24. Can be a problem that they are all install in one folder "Java" but in separate folders.

                    I am not sure what Additional options I have to add in the C project adjustments. I add only -m32 and -shared, but in a few other samples a lot more were add. Maybe I have to add something more there in order to build the correct dll file?
                    • 22. Re: JNI sample problem
                      EJP
                      How can I produce map file?
                      It's a linker option. You'll have to look it up. I don't know what linker you're using and in any case it's not a Java question.
                      I add two directories to the c compiler - the include one and the win32 one from the java developer kit. I have four versions of it - three versions 6.0 and one version 6.0_24. Can be a problem that they are all install in one folder "Java" but in separate folders.
                      Use the latest one, or else the one from the JVM that you are targeting.
                      Maybe I have to add something more there in order to build the correct dll file?
                      Maybe so. Again it's not a Java question. But I will add that I have never had success trying to use a shared C runtime with JNI DLLs.
                      • 23. Re: JNI sample problem
                        jschellSomeoneStoleMyAlias
                        As posted earlier...

                        The h file

                        JNIEXPORT void JNICALL Java_helloworld_Main_nativePrint

                        The source file.

                        JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint


                        Those do not match.
                        • 24. Re: JNI sample problem
                          837475
                          That's because I try to different examples, In fact it is:

                          header file code:
                          /* DO NOT EDIT THIS FILE - it is machine generated */
                          #include <jni.h>
                          /* Header for class jnidemojava_Main */
                          
                          #ifndef _Included_jnidemojava_Main
                          #define _Included_jnidemojava_Main
                          #ifdef __cplusplus
                          extern "C" {
                          #endif
                          /*
                           * Class:     jnidemojava_Main
                           * Method:    nativePrint
                           * Signature: ()V
                           */
                          JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
                            (JNIEnv *, jobject);
                          
                          #ifdef __cplusplus
                          }
                          #endif
                          #endif
                          and the source file:
                          #include <jni.h>
                          #include <stdio.h>
                          #include "JNIDemoJava.h"
                          
                          JNIEXPORT void JNICALL Java_jnidemojava_Main_nativePrint
                                  (JNIEnv *env, jobject obj)
                          {
                          
                              printf("\nHello World from C\n");
                          
                          }
                          • 25. Re: JNI sample problem
                            888281
                            Hello,

                            You can try this example.
                            This gives you full explanation of JNI on Windows.

                            Thanks
                            1 2 Previous Next