Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling an external C function from a C file in JNI

843829Jun 4 2010 — edited Jul 13 2010
Hello,

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 10 2010
Added on Jun 4 2010
4 comments
501 views