JNI: Calling an outside function in the C file which is being called by the Java file

Joined
Jun 4, 2010
Messages
1
Reaction score
0
Hello Guys,

I am trying to use JNI, to call an outside function in a C file, which is being called by a Java file. Here is my code:

This is HelloWorld.c

#include "MyOldHello.h"
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
helloPrint();
return;
}

This is MyOldHello.c

#include <jni.h>
#include <stdio.h>
#include "MyOldHello.h"

helloPrint()
{
printf("\nHello World!\n");
return;
}

This is MyOldHello.h

void helloPrint();

And this is HelloWorld.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


and finally my HelloWorld.java

class HelloWorld
{
private native void print();

public static void main(String[] args)
{
new HelloWorld().print();
}

static
{
System.loadLibrary("HelloWorld");
}
}

So basically this is what I type into the Visual Studio Command Prompt:

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 this is the error I get:
HelloWorld.obj : erroe LNK 2019: unresolved external symbol _helloPrint referenced in function _Java_HelloWorld_print@8
HelloWorld.dll : fatal error LNK1120: 1 unresolved externals

If someone could help me out how to call an external function from a C file which is being called from a Java file using JNI it would greatly help me.

Thanks
Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top