An odd JNI string-problem when trying to send(Java)-change(C++)-sendback(Java)

  • Thread starter kornkreismuster
  • Start date
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
 
G

Gordon Beaton

I encountered a very strange problem, when I tried to understand how
JNI works with strings.
[...]

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!

These are not JNI issues at all.

Your problem is that neither you nor your senior programmer seem to
understand that C and C++ are different programming languages! You
claim to be coding in C++, but your code is in fact C.

In C you must declare local variables at the start of a block (such as
a function), before any code statements in that block. Once you add a
statement (in this case printf()), you have ended the declaration
section. Additional variable declarations after the statement will
cause the type of errors you are experiencing.

/gordon
 
K

kornkreismuster

Well, I think we all love computers, because we love to feel like a
dumb ass!

Thanks a lot!
 
J

John Ersatznom

Gordon said:
Your problem is that neither you nor your senior programmer seem to
understand that C and C++ are different programming languages! You
claim to be coding in C++, but your code is in fact C.

In C you must declare local variables at the start of a block (such as
a function), before any code statements in that block. Once you add a
statement (in this case printf()), you have ended the declaration
section. Additional variable declarations after the statement will
cause the type of errors you are experiencing.

Easier still: rename the file to end in .cc instead of .c. Then the C++
compiler will actually compile it as C++. Problem solved. :)
 
G

Gordon Beaton

Easier still: rename the file to end in .cc instead of .c. Then the
C++ compiler will actually compile it as C++. Problem solved. :)

Well, no. The OP code is C and will not compile as C++, regardless of
what the files are called. Once again, they aren't the same language.

/gordon
 
J

John Ersatznom

Gordon said:
Well, no. The OP code is C and will not compile as C++, regardless of
what the files are called. Once again, they aren't the same language.

C is (almost) a subset of C++. There are some pointer casting
requirements and reserved word differences to watch out for of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top