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

Discussion in 'Java' started by kornkreismuster, Jan 4, 2007.

  1. 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
     
    kornkreismuster, Jan 4, 2007
    #1
    1. Advertising

  2. On 4 Jan 2007 07:24:57 -0800, kornkreismuster wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Jan 4, 2007
    #2
    1. Advertising

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

    Thanks a lot!
     
    kornkreismuster, Jan 5, 2007
    #3
  4. Gordon Beaton wrote:
    > 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. :)
     
    John Ersatznom, Jan 5, 2007
    #4
  5. On Fri, 05 Jan 2007 15:31:33 -0500, John Ersatznom wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Jan 5, 2007
    #5
  6. Gordon Beaton wrote:
    > On Fri, 05 Jan 2007 15:31:33 -0500, John Ersatznom wrote:
    >
    >>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.
     
    John Ersatznom, Jan 8, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    851
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    906
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,702
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    623
    vasanth
    Jan 25, 2005
  5. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,104
    Richard Heathfield
    Feb 18, 2007
Loading...

Share This Page