Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This sample program is meant to call a native method written in C.

Java Code

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.

The header file generated is :

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <C:Program FilesJavajdk1.7.0includejni.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

#include <C:Program FilesJavajdk1.7.0includejni.h>
#include <C:Program FilesJavajdk1.7.0includewin32jni_md.h>
#include <stdio.h>
#include "HelloWorld.h"

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

The error I get on compiling is fatal error C1083: Cannot open include file: 'jni_md.h': No such file or directory

Also my compiler underlines jobject obj saying that this class does not have storage class or specifier . It underlines *env saying expected a ')'.

Why do I get this error ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
409 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

Try adding both of these entries to your C compiler's include path:

  • C:Program FilesJavajdk1.7.0include
  • C:Program FilesJavajdk1.7.0includewin32

The win32 path might not be necessary, depending on how jni.h is set up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...