Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
kornkreismuster - 04 Jan 2007 15:24 GMT
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
Gordon Beaton - 04 Jan 2007 15:43 GMT
> 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
[quoted text clipped - 7 lines]
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

kornkreismuster - 05 Jan 2007 11:48 GMT
Well, I think we all love computers, because we love to feel like a
dumb a.s!

Thanks a lot!
John Ersatznom - 05 Jan 2007 20:31 GMT
> Your problem is that neither you nor your senior programmer seem to
> understand that C and C++ are different programming languages! You
[quoted text clipped - 5 lines]
> 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. :)
Gordon Beaton - 05 Jan 2007 20:50 GMT
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

John Ersatznom - 08 Jan 2007 07:44 GMT
>>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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.