Getting Started with JNI and C under Windows

2019-06-26 03:57发布

I'm new in Java learning and first time want to get start JNI. And I am working with Cygwin and I have created a file with .java (Helloworld.java) extension as follows:

class HelloWorld {
     private native void print();
     public static void main(String[] args) {
         new HelloWorld().print();
     }
     static {
         System.loadLibrary("HelloWorld");
     }
 }

Then I compile the file through the command line (javac Helloworld.java) after that create a native header file through command javah –jni Helloworld Then also implement by creating a C file as:

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

 JNIEXPORT void JNICALL 
 Java_HelloWorld_print(JNIEnv *env, jobject obj)
 {
     printf("Hello World!\n");
     return;
 }

After that while I run the command

 Gcc –wall –g Helloworld.c –o hello 

Give the message in command prompt line in Cygwin

 helloworld.c:1:17: fatal error: jni.h: No such file or directory
 compilation terminated.

How to run and what is the procedure to set the path to execute the file? Please help me any one. Thanks.

4条回答
该账号已被封号
2楼-- · 2019-06-26 04:17

I used MinGW on Windows. After adding ";C:\MinGW\bin\" to the Path Environment variable, I was able to run gcc.exe from the Windows command prompt using the command below.

The jni.h file is located in java home include folder. You can use the JAVA_HOME environment variable or specify explicitly.

 gcc -c -I"C:\Program Files\Java\jdk1.6.0_26\include" -I"C:\Program Files\Java\jdk1.6.0_26\include\win32" "S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.c"

This created a HelloWorld.o file.

查看更多
干净又极端
3楼-- · 2019-06-26 04:18

I guess you haven't added the jni directory to the include directory list, check http://social.msdn.microsoft.com/forums/is/vclanguage/thread/cd7f2d1d-f750-494c-a3b2-7d4186cfe51c for details.

查看更多
别忘想泡老子
4楼-- · 2019-06-26 04:24

Assuming that you installed a JDK, you need to inform gcc about the includes directory for JNI:

-I$JAVA_HOME/../include

where $JAVA_HOME should be set as the parent directory of the Java bin directory (/usr/lib/jvm/java-6-openjdk-amd64/jre on my machine, for example). If it is not set, well, it is simpler if you just locate the folder where your jni.h header resides.

I agree that to start learning Java from JNI is a daunting task, but I respect your decision. If you fashion books, I would suggest Core Java Volume I and Core Java Volume II. At the end of the latter, in particular, you can find a pretty exhaustive coverage of native methods.

查看更多
我命由我不由天
5楼-- · 2019-06-26 04:37

If Visual Studio Successfully installed on your machine, You can compile using visual studio developer command prompt.

To produce X86 (32 bit) version dll use "VS20XX x86 Native Tools Command Prompt"

To produce X64 (64 bit) version dll use "VS20XX x64 Native Tools Command Prompt"

XX in VS20XX is visual studio version 2008 and above.

cl.exe /LD /EHsc /I "%JAVA_HOME%\include" /I "%JAVA_HOME%\include\win32" TestJNIString.c

Note: Make sure JAVA_HOME variable is set in environment variable.

查看更多
登录 后发表回答