This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Jun 7, 2013 6:16 PM by EJP RSS

JNI with Visual C++

ananddrox Newbie
Currently Being Moderated
Hi,

I am a newbie. I have a code using Visual C++ with integrated libraries. Can I interface it with Java ?

Regards,

D. Anand Kumar
  • 1. Re: JNI with Visual C++
    1010920 Newbie
    Currently Being Moderated
    What do you mean about "interface it with Java"?

    I tried
    1, invoking native library from java
    2, invoking java library from native code

    both work. What you need is just went through the JNI tutorial.

    [http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html|http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html]

    [http://www.ibm.com/developerworks/java/tutorials/j-jni/|http://www.ibm.com/developerworks/java/tutorials/j-jni/]

    [https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html|https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html]
  • 2. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    Hi,

    I can explain more on the question. I would like to write a program in Java which calls a native method that's described in a C++ program.

    Sample.java

    public class Sample
    {
         static
         {
              System.loadLibrary("Sample");
         }
         // A native method that receives nothing and returns void
         private native void test();
         public static void main(String[] args)
         {
              new Sample().test(); // invoke the native method
         }
    }

    Sample.cpp

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    void test() // native function
    {
    // function definition
    }


    I compiled and builded the Sample.cpp in a Visual C++ environment into a .dll. Now I tried to execute the java program with this dll and it said it could not find dependent libraries. I tried the following too.

    #include <jni.h>
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    JNIEXPORT void JNICALL Java_RUI_test(JNIEnv *env, jobject thisObj)
    {
    //code
    }

    I added additional dependencies to the program to include the jni header from JAVA_HOME/include and JAVA_HOME/include/Win32. I builded a dll and tried to execute the java code and hit again at the same error "Unsatisfied Link Error : Cant find dependent libraries".
  • 3. Re: JNI with Visual C++
    gimbal2 Guru
    Currently Being Moderated
    So perhaps Java isn't lying and it really cannot find the library. How are you running the java application? Where do you put the native DLL? Are you sure you are compiling the DLL for the correct CPU architecture that matches your Java runtime (32 bits/64 bits) ?
  • 4. Re: JNI with Visual C++
    EJP Guru
    Currently Being Moderated
    You need to include the generated .h file in your .cpp file.
    Cant find dependent libraries
    That means that a secondary DLL your DLL is linked to was not found.
  • 5. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    I have added the header too. I just missed it in the code above.
  • 6. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    I think I am running in the correct environment.

    But i tried to write the code in a cpp file and tried to generate a dll offshore out of Visual C++ environment. I obviously had to have the libraries and hence linked them during execution to form the dll.

    But I am unsure of the libraries. I am linking them during execution but I get errors like

    xstring:2011:20:error: const class std::basic_string(cahr,std::char_traits<char>.................................) has no member named '_Myres' . This is the last line of a two-to-three page running errors. I think there is some problem with the headers and libraries I use for execution.
  • 7. Re: JNI with Visual C++
    EJP Guru
    Currently Being Moderated
    Err, you need to sort out all your compile and link errors before you try to execute.
  • 8. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    I think there is some ambiguity in my previous statements. To be more clear, I am trying to generate a dll first and then going to link it to execute the java program. Now the error I recently posted is for the formation of dll file. I cant generate a dll first of all. After that only, I can jump into execution of the java program.

    Edited by: 1010268 on Jun 7, 2013 2:13 AM
  • 9. Re: JNI with Visual C++
    EJP Guru
    Currently Being Moderated
    You said in your second post that you 'tried to execute the Java code'. Now you are talking about link-time errors, possibly even compile-time errors. This isn't 'ambiguous', it's just wrong.
  • 10. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    Sorry about that. I guess I was typing a lot. To make things clear,

    I compiled the cpp code into a dll in Visual C++ environment and tried to link it during execution of the java code which gave me "Cant find dependent libraries".

    Then I tried to write the code offline in Notepad and tried to link dependent libraries to compile the code into a dll which raised a two-to-three page running error. Sample error is "xstring:2011:20:error: const class std::basic_string(cahr,std::char_traits<char>.................................) has no member named '_Myres' ".

    So basically I tried it in two different ways and got two different errors.
  • 11. Re: JNI with Visual C++
    EJP Guru
    Currently Being Moderated
    What does 'link it during execution of the Java code' mean? You're running the linker under control of Java?
  • 12. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    I referred this link :

    https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

    'link it during execution of the Java code' means java -Djava.library.path=C:\xxx\hello.dll HelloJNI from the link above.
  • 13. Re: JNI with Visual C++
    EJP Guru
    Currently Being Moderated
    So you just mean dynamic linking? And these are dynamic link errors? They sure don't look like it.
  • 14. Re: JNI with Visual C++
    ananddrox Newbie
    Currently Being Moderated
    I got "Cant find dependent libraries" error when I dynamically linked the Visual C++ generated dll to execute the java program.

    I got a two page error (sample - xstring:2011:20:error: const class std::basic_string(cahr,std::char_traits<char>.................................) has no member named '_Myres' ) when I try to link or include the libraries and dependencies required to compile the cpp code into a dll.
    *g++ --options -I"Path to headers and libraries required for compiling the cpp code" File.cpp*

    Edited by: 1010268 on Jun 7, 2013 3:51 AM
1 2 Previous Next

Legend

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