This content has been marked as final. Show 16 replies
What do you mean about "interface it with Java"?
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.
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.
public class 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
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.
using namespace std;
JNIEXPORT void JNICALL Java_RUI_test(JNIEnv *env, jobject thisObj)
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".
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) ?
You need to include the generated .h file in your .cpp file.
Cant find dependent librariesThat means that a secondary DLL your DLL is linked to was not found.
I have added the header too. I just missed it in the code above.
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.
Err, you need to sort out all your compile and link errors before you try to execute.
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
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.
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.
What does 'link it during execution of the Java code' mean? You're running the linker under control of Java?
I referred this link :
'link it during execution of the Java code' means java -Djava.library.path=C:\xxx\hello.dll HelloJNI from the link above.
So you just mean dynamic linking? And these are dynamic link errors? They sure don't look like it.
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