Calling an external C function from a C file in JNI
843829Jun 4 2010 — edited Jul 13 2010Hello,
I am trying to call an external C function, from a C file which is being called by a Java file. I am getting an unresolved symbol error. I have tried many things like, making the external C function in the format of JNI, etc, etc. It is still not working. I think it has something to do with linking the two C files. If anyone can please answer this, it would greatly help me. here is the code:
HelloWorld.c:
#include <jni.h>
#include <stdio.h>
#include "MyOldHello.h"
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
helloPrint();
return;
}
HelloWorld.java:
class HelloWorld
{
private native void print();
public static void main(String[] args)
{
new HelloWorld().print();
}
static
{
System.loadLibrary("HelloWorld");
System.loadLibrary("MyOldHello");
}
}
MyOldHello.c:
#include <jni.h>
#include <stdio.h>
#include "MyOldHello.h"
void helloPrint()
{
printf("\nHello World!\n");
}
MyOldHello.h:
void helloPrint();
Now i use the Visual C++ command prompt to compile this by saying:
javac HelloWorld.java
javah -jni HelloWorld
cl -Ic:\Java\jdk1.6.0_20\include -Ic:\Java\jdk1.6.0_20\include\win32 -MD -LD HelloWorld.c -FeHelloWorld.dll
and now it gives me the error saying that there is an unresolved external symbol, which is the call to helloPrint in the file HelloWorld.
If anyone knows how to solve this, or how to call external C functions from a C file that is being called from a Java file using JNI, please respond.
Thanks
Nick