I am trying to create a simple example with JNI. I am having trouble compiling the .cpp source file. I will give all the steps that I have done/tried below. I am trying to follow the tutorial found here: http://java.sun.com/docs/books/jni/html/start.html#27008
I have a Java program called HelloJNI.java
public class HelloJNI
{
private native void print();
public static void main(String[] args)
{
new HelloJNI().print();
}
static
{
System.loadLibrary("HelloJNI");
}
}
From here I compiled the java file and called
javah -jni HelloJNI to generate HelloJNI.h
From here I create the .cpp source file
#include <jni.h>
#include <iostream>
#include "HelloJNI.h"
using namespace std;
JNIEXPORT void JNICALL
Java_HelloJNI_print(JNIEnv *env, jobject obj)
{
cout << "Hello JNI!" << endl;
return;
}
Now that I have all of that I try to create the .dll from the source file, I am using this command to run gcc on cygwin (found this command here – http://www.inonit.com/cygwin/jni/helloWorld/c.html):
gcc -mno-cygwin -I$JAVA_HOME/include -I$JAVA_HOME/include/win32
-Wl,--add-stdcall-alias -shared -o HelloJNI.dll HelloJNI.c
When I do this I get an error:
HelloJNI.cpp:1:17: fatal error: jni.h: No such file or directory
compilation terminated.
This is where I am stuck, I don’t really know whey the compiler can’t find jni.h it is in the $JAVA_HOME/include directory.
Results from ls $JAVA_HOME/include:
classfile_constants.h jdwpTransport.h jvmti.h win32
jawt.h jni.h jvmticmlr.h
I know it is a lengthy post, but any help would be awesome.
Thanks
I use the following flags to compile:
However, I should mention that I was not able to run my JNI application if the dll was compiled with cygwin gcc. I compiled with the Visual Studio then and it worked.