Getting Started with JNI and C under Windows

旧巷老猫 提交于 2019-12-14 04:25:20

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/11154020/getting-started-with-jni-and-c-under-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!