Subject: Java JNI with GNU-WIN32 gcc
From: [email-masked] (Andrew Mickish)

12 Jun 1998 01:49:30 -0700
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
To: [email-masked]

With significant help from Bill Pringlemeir, I figured out how to compile
Sun's JNI tutorial HelloWorld example under GNU-WIN32 with gcc.  I will keep a
ZIP file of the gcc-compatible code at
http://www.andrew.cmu.edu/~am2q/HelloWorld.zip for future reference.

Please add this information to the GNU-WIN32 documentation and FAQ.  As of
version 19.1, it is significantly more complicated to compile a
Java-compatible DLL under gcc than under Microsoft Visual C++.  Even printf()
does not work in a gcc-compiled DLL, even thoough it works fine with VC++.
Will this be changing with future releases of GNU-WIN32?

Here are a few hints for how to compile a trivial JNI-compaible DLL with gcc:

1)  You need init.cc, an element of building relocatable DLLs

2)  There is a complicated linking procedure involving ld and intermediate
files:

     ld --base-file hello.base --dll -o hello.dll HelloWorldImp.o init.o
$(LIB) -e _dll_entry@12
     dlltool --as=as --dllname hello.dll --def hello.def --base-file
hello.base --output-exp hello.exp
     ld --base-file hello.base hello.exp --dll -o hello.dll HelloWorldImp.o
init.o $(LIB) -e _dll_entry@12
     dlltool --as=as --dllname hello.dll --def hello.def --base-file
hello.base --output-exp hello.exp
     ld hello.exp --dll -o hello.dll HelloWorldImp.o init.o $(LIB) -e
_dll_entry@12

3)  Printf will not work here.  Use a Windows dialog box or call back into
Java to invoke
     System.out.println():

    void jprintf (JNIEnv *env, char* str) {

      jstring jstr = (*env)->NewStringUTF(env, str);
      jclass System_class = (*env)->FindClass(env, "java/lang/System");
      jfieldID fid_out = (*env)->GetStaticFieldID(env, System_class, "out",
"Ljava/io/PrintStream;");
      jobject out = (*env)->GetStaticObjectField(env, System_class, fid_out);
      jclass PrintStream_class = (*env)->FindClass(env,
"java/io/PrintStream");
      jmethodID mid_println = (*env)->GetMethodID(env, PrintStream_class,
"println", "(Ljava/lang/String;)V");
      if (mid_println == 0) {
        exit(1);
      }
      (*env)->CallVoidMethod(env, out, mid_println, jstr);
    }

    JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env,
jobject obj)
    {
      (void)env; (void)obj;
      MessageBoxA(NULL,"Hello Jupiter!", "Hello world!", 0);
      jprintf(env, "Hello Uranus!\n");
      return;
    }

A collection of files that implement Sun's JNI HelloWorld example may be found
at http://www.andrew.cmu.edu/~am2q/HelloWorld.zip

--Andrew Mickish
  [email-masked]

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".