c++ iostream not working through JNI


I am creating a shared library Client.dll using Cygwin g++ compiler and calling through a JAVA program.

Everything is working fine if I remove 'cout <<' command from Client.c program. The method returns the value. But when I add cout command the JVM freezes.

Client.c has :

JNIEXPORT jint JNICALL Java_nativeCode_Client_getDummyData(JNIEnv * env, jobject obj, jint pmuId)
{
jint myint = 11;
pmuId = pmuId + pmuId;
cout <<"here";
return myint;
}

I am creating .dll as,

$ g++ -I"C:\Program Files (x86)\Java\jdk1.6.0_33\include" -I"C:\Program Files (x86)\Java\jdk1.6.0_33\include\win32" -Wl,--add-stdcall-alias -shared -o Client.dll Client.c

What am I doing wrong?

Thanks in advance.
A DLL is not guaranteed to have a console when run, and even if it does, maybe the variable cout is not properly linked for starters. Is this Java program of yours providing a console?
yes....I have created an eclipse Java project that is accessing the DLL.
Ok, so then most likely the variable std::cout is not linked to the console provided by the Java application.

According to http://www.daniweb.com/software-development/cpp/threads/347901/is-it-possible-to-use-a-console-app-and-windows-one-at-the-same-time , this code should attach the variables to the console:

1
2
3
4
5
6
7
8
9
10
    if (AllocConsole()) {
        int ifd = _open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
        int ofd = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
        *stdin = *_fdopen(ifd, "r");
        *stdout = *_fdopen(ofd, "w");
        std::cout<<"I made a console window";
        std::cin.get();
        fclose(stdout);
        fclose(stdin);
    }


Try out the sample in the link to see if it indeed works. If it does, simply clean it up to make it suitable for a DllMain() or some Init() function.
Topic archived. No new replies allowed.