This sample program is meant to call a native
method written in C.
这个示例程序的目的是调用用C编写的本机方法。
Java Code
Java代码
class HelloWorld {
private native void print();
public static void main( String args[] ) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
After writing this i compiled the program and generated a JNI
style header file.
编写完这个程序后,我编译了这个程序并生成了一个JNI样式的头文件。
The header file generated is :
生成的头文件为:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <C:\Program Files\Java\jdk1.7.0\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 the native method written in c
用c写的原生方法
#include <C:\Program Files\Java\jdk1.7.0\include\jni.h>
#include <C:\Program Files\Java\jdk1.7.0\include\win32\jni_md.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_print( JNIENv *env , jobject obj) {
printf("Hello World!\n");
return;
}
The error I get on compiling is fatal error C1083: Cannot open include file: 'jni_md.h': No such file or directory
我在编译时得到的错误是致命错误C1083:无法打开include文件:'jni_md。没有这样的文件或目录。
Also my compiler underlines jobject obj
saying that this class does not have storage class or specifier
. It underlines *env
saying expected a ')'
.
我的编译器也强调jobject obj说这个类没有存储类或说明符。它强调*env表示“预期a”)。
Why do I get this error ?
为什么会出现这个错误?
4 个解决方案
#1
34
I suspect that jni.h
is trying to #include <jni_md.h>
, which is then failing because you haven't added its location to your include path.
我怀疑jni。h试图#include
Try adding both of these entries to your C compiler's include path:
尝试将这两个条目添加到C编译器的include路径:
C:\Program Files\Java\jdk1.7.0\include
- C:\Program Files\Java\jdk1.7.0\include
C:\Program Files\Java\jdk1.7.0\include\win32
- C:\Program Files\Java\jdk1.7.0\include\win32
The win32
path might not be necessary, depending on how jni.h
is set up.
根据jni的方式,可能不需要win32路径。h是设置。
#2
0
Try this,
试试这个,
HelloWorld.c
HelloWorld.c
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
Compile it using cl.exe (I'm using VC++ and CL.EXE required following command line switches.)
使用cl编译它。exe(我用的是vc++和CL。EXE需要以下命令行切换)
c:\>cl -c /I"c:\Program Files\java\jdk1.7.0\include" /I"c:\Prog ram Files\java\jdk1.7.0\include\win32" HelloWorld.c
\>cl -c /I"c:\程序文件\java\jdk1.7.0\包含" /I"c:\Program文件\java\jdk1.7.0\include win32" helloworldc
Link .obj module
链接.obj模块
c:\>link /libpath="c:\Program Files\java\jdk1.7.0\lib" HelloWorld.obj /dll
c:\ >链接/ libpath = " c:\ Program Files \ java \ jdk1.7.0 \ lib”HelloWorld。obj / dll
#3
0
I had this issue once, my solution was actually to edit the jni.h's internal #include from "jni_md.h"
to "win32/jni_md.h"
, although there is probably a less hacky way you are supposed to do it.
我曾经遇到过这个问题,我的解决方案实际上是编辑jni。h的内部#include来自“jni_md”。h”到“win32 / jni_md。h,尽管你应该用一种不太常见的方式来做这件事。
#4
-2
Just copy jni_md.h file to folder where jni.h.
只是jni_md副本。h文件到jni.h的文件夹。
#1
34
I suspect that jni.h
is trying to #include <jni_md.h>
, which is then failing because you haven't added its location to your include path.
我怀疑jni。h试图#include
Try adding both of these entries to your C compiler's include path:
尝试将这两个条目添加到C编译器的include路径:
C:\Program Files\Java\jdk1.7.0\include
- C:\Program Files\Java\jdk1.7.0\include
C:\Program Files\Java\jdk1.7.0\include\win32
- C:\Program Files\Java\jdk1.7.0\include\win32
The win32
path might not be necessary, depending on how jni.h
is set up.
根据jni的方式,可能不需要win32路径。h是设置。
#2
0
Try this,
试试这个,
HelloWorld.c
HelloWorld.c
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
Compile it using cl.exe (I'm using VC++ and CL.EXE required following command line switches.)
使用cl编译它。exe(我用的是vc++和CL。EXE需要以下命令行切换)
c:\>cl -c /I"c:\Program Files\java\jdk1.7.0\include" /I"c:\Prog ram Files\java\jdk1.7.0\include\win32" HelloWorld.c
\>cl -c /I"c:\程序文件\java\jdk1.7.0\包含" /I"c:\Program文件\java\jdk1.7.0\include win32" helloworldc
Link .obj module
链接.obj模块
c:\>link /libpath="c:\Program Files\java\jdk1.7.0\lib" HelloWorld.obj /dll
c:\ >链接/ libpath = " c:\ Program Files \ java \ jdk1.7.0 \ lib”HelloWorld。obj / dll
#3
0
I had this issue once, my solution was actually to edit the jni.h's internal #include from "jni_md.h"
to "win32/jni_md.h"
, although there is probably a less hacky way you are supposed to do it.
我曾经遇到过这个问题,我的解决方案实际上是编辑jni。h的内部#include来自“jni_md”。h”到“win32 / jni_md。h,尽管你应该用一种不太常见的方式来做这件事。
#4
-2
Just copy jni_md.h file to folder where jni.h.
只是jni_md副本。h文件到jni.h的文件夹。