K
kornkreismuster
Hi!
I encountered a very strange problem, when I tried to understand how
JNI works with strings.
Let me beginn with some code that does work!
--- START: Code that does work! ---
#include <stdio.h>
#include <jni.h>
#include "HelloCpp.h"
JNIEXPORT jstring JNICALL Java_HelloCpp_jnipowwa(JNIEnv *env, jobject
obj, jstring sOfJ){
const char *c_Str = (*env)->GetStringUTFChars(env, sOfJ, 0);
jstring sOfC = (*env)->NewStringUTF(env,c_Str);
return sOfC;
}
--- END: Code that does work ---
The string is send out of Java to C++, converted into a char[],
converted into a jstring and send back to Java. No problem about that!
Now I just do a small step for me and an even smaller step for mankind:
I add a printf("") between the commands.
So after it looks like this:
--- START: Add a printf ---
#include <stdio.h>
#include <jni.h>
#include "HelloCpp.h"
JNIEXPORT jstring JNICALL Java_HelloCpp_jnipowwa(JNIEnv *env, jobject
obj, jstring sOfJ){
const char *c_Str = (*env)->GetStringUTFChars(env, sOfJ, 0);
printf("");
jstring sOfC = (*env)->NewStringUTF(env,c_Str);
return sOfC;
}
--- END: Add a printf ---
When I follow my wish to compile it I get some very funny strings right
out of my compiler:
--- START: Very funny strings ---
HelloCpp.c
HelloCpp.c(19) : error C2275: 'jstring' : illegal use of this type as
an expression
C:\Program Files\Java\jdk1.6.0\include\jni.h(86) : see
declaration of 'jstring'
HelloCpp.c(19) : error C2146: syntax error : missing ';' before
identifier 'sOfC'
HelloCpp.c(19) : error C2144: syntax error : '<Unknown>' should be
preceded by '<Unknown>'
HelloCpp.c(19) : error C2144: syntax error : '<Unknown>' should be
preceded by '<Unknown>'
HelloCpp.c(19) : error C2143: syntax error : missing ';' before
'identifier'
HelloCpp.c(19) : error C2065: 'sOfC' : undeclared identifier
HelloCpp.c(19) : warning C4047: '=' : 'int' differs in levels of
indirection from 'jstring'
HelloCpp.c(21) : warning C4047: 'return' : 'jstring' differs in levels
of indirection from 'int'
--- END: Very funny strings ---
If you are confused now: You're not alone!
printf is not the only thing disturbing my compiler. Nearly every
command inserted into the code - even a for-to loop - is sending me
some errormessages, no matter where the command is written in the
function.
It doesn't harm the compiler when I insert some definitions like "int =
x" into the code instead of the printf.
Maybe someone of you can help me.
Our senior programmer wasn't able.
So, your chance to be better than our senior!
Thx & Peace!
My System:
Windows XP Prof SP2
Java: j2sdk-1_4_2_13
MS Development Environment 2003 Version 7.1.3088
MS .NET Framework 1.1 Version 1.1.4322
I encountered a very strange problem, when I tried to understand how
JNI works with strings.
Let me beginn with some code that does work!
--- START: Code that does work! ---
#include <stdio.h>
#include <jni.h>
#include "HelloCpp.h"
JNIEXPORT jstring JNICALL Java_HelloCpp_jnipowwa(JNIEnv *env, jobject
obj, jstring sOfJ){
const char *c_Str = (*env)->GetStringUTFChars(env, sOfJ, 0);
jstring sOfC = (*env)->NewStringUTF(env,c_Str);
return sOfC;
}
--- END: Code that does work ---
The string is send out of Java to C++, converted into a char[],
converted into a jstring and send back to Java. No problem about that!
Now I just do a small step for me and an even smaller step for mankind:
I add a printf("") between the commands.
So after it looks like this:
--- START: Add a printf ---
#include <stdio.h>
#include <jni.h>
#include "HelloCpp.h"
JNIEXPORT jstring JNICALL Java_HelloCpp_jnipowwa(JNIEnv *env, jobject
obj, jstring sOfJ){
const char *c_Str = (*env)->GetStringUTFChars(env, sOfJ, 0);
printf("");
jstring sOfC = (*env)->NewStringUTF(env,c_Str);
return sOfC;
}
--- END: Add a printf ---
When I follow my wish to compile it I get some very funny strings right
out of my compiler:
--- START: Very funny strings ---
HelloCpp.c
HelloCpp.c(19) : error C2275: 'jstring' : illegal use of this type as
an expression
C:\Program Files\Java\jdk1.6.0\include\jni.h(86) : see
declaration of 'jstring'
HelloCpp.c(19) : error C2146: syntax error : missing ';' before
identifier 'sOfC'
HelloCpp.c(19) : error C2144: syntax error : '<Unknown>' should be
preceded by '<Unknown>'
HelloCpp.c(19) : error C2144: syntax error : '<Unknown>' should be
preceded by '<Unknown>'
HelloCpp.c(19) : error C2143: syntax error : missing ';' before
'identifier'
HelloCpp.c(19) : error C2065: 'sOfC' : undeclared identifier
HelloCpp.c(19) : warning C4047: '=' : 'int' differs in levels of
indirection from 'jstring'
HelloCpp.c(21) : warning C4047: 'return' : 'jstring' differs in levels
of indirection from 'int'
--- END: Very funny strings ---
If you are confused now: You're not alone!
printf is not the only thing disturbing my compiler. Nearly every
command inserted into the code - even a for-to loop - is sending me
some errormessages, no matter where the command is written in the
function.
It doesn't harm the compiler when I insert some definitions like "int =
x" into the code instead of the printf.
Maybe someone of you can help me.
Our senior programmer wasn't able.
So, your chance to be better than our senior!
Thx & Peace!
My System:
Windows XP Prof SP2
Java: j2sdk-1_4_2_13
MS Development Environment 2003 Version 7.1.3088
MS .NET Framework 1.1 Version 1.1.4322