JNI problem

Hi,
I have a C++ JNI problem. Below code doesn't compile in VC++ (VS2010).
It's part of a C++ dll, that's called from a Java program.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <jni.h>
#include <stdio.h>
#include "TestJNIString.h"
JNIEXPORT jstring JNICALL Java_TestJNIString_sayHello(JNIEnv *env, jobject thisObj, jstring inJNIStr)  {
    const char *inCStr = env->GetStringUTFChars(inJNIStr, NULL);
    if (NULL == inCSt) return NULL;
    printf("In C, the received string is: %s\n", inCStr);
    env->ReleaseStringUTFChars(inJNIStr, inCStr);
    char outCStr[128];
    printf("Enter a String: ");
    scanf("%s", outCStr);
    return env->NewStringUTF(outCStr);
}


However building it in VC++ gives a number of errors :

Error 1 error C2223: left of '->GetStringUTFChars' must point to struct/union
Error 2 error C2223: left of '->ReleaseStringUTFChars' must point to struct/union
Error 5 error C2223: left of '->NewStringUTF' must point to struct/union

On "char outCStr[128];" :
Error 3 error C2143: syntax error : missing ';' before 'type'

Error 4 error C2065: 'outCStr' : undeclared identifier

Do anyone know how to fix these ? Thanks in advance
At the risk of being CaptiainObvious, the compiler doesn't like something about JNIEnv *. The compiler apparently found JNIEnv, or it would have complained when parsing the function header.

http://xdprof.sourceforge.net/doxygen/jni_8h-source.html shows JNIEnv to be a typedef of an incomplete struct (JNIENV_).


00168 struct JNIEnv_;
00169
00170 #ifdef __cplusplus
00171 typedef JNIEnv_ JNIEnv;
00172 #else
00173 typedef const struct JNINativeInterface_ *JNIEnv;
00174 #endif


Since the struct type is incomplete, I don't see how the compiler is going to resolve your function calls through the env pointer.

The other two errors I'm assuming happened only because the compiler go out of sync parsing.

http://xdprof.sourceforge.net/doxygen/jni_8h-source.html shows JNIEnv to be a typedef of an incomplete struct (JNIENV_).


It is not incomplete, it is just forward declared. Just scroll a few lines down for the actual declaration.

The only possible reason I can think of the error is that there must be some preprocessor macro switches affecting the compilation. I am not familiar with Visual Studio, but it maybe compiling the source file as C code?
AbstractionAnon:

It's typedef'd

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifdef __cplusplus
typedef JNIEnv_ JNIEnv;
#else
typedef const struct JNINativeInterface_ *JNIEnv;
#endif

struct JNIEnv_ {
    const struct JNINativeInterface_ *functions;
#ifdef __cplusplus
    const char* GetStringUTFChars(jstring str, jboolean *isCopy) {
        return functions->GetStringUTFChars(this,str,isCopy);
    }
#endif 
Vince1027:

I made an empty C++ projekt in Visual Studio, and manually added the header and the sourcecode. The sourcecode was at first in C ... but via the Intellisence of C++ I came up with the posted code. Very strange that code added via the Intellisence gives compile-errors later on.

Anybody know how to solve it ? I could post the header and source file for somebody who wants to give it a try.
nobody ?

This is "TestJNIString.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestJNIString */

#ifndef _Included_TestJNIString
#define _Included_TestJNIString
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     TestJNIString
 * Method:    sayHello
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_TestJNIString_sayHello
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif 





Topic archived. No new replies allowed.