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

1 Jul 1998 07:55:58 -0700
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="------------ABE44DF5F82EB94F222D734B"
To: [email-masked]

This is a multi-part message in MIME format.
--------------ABE44DF5F82EB94F222D734B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Here are a few more issues that came up when trying to compile a JNI DLL with
gcc.

*  There are two different problems that could cause Unsatisfied link errors.
One is
if the DLL is not found, and the other is if the DLL is found but is
unreadable or
the specific function cannot be found in it.  These problems are
distinguishable by
the stack trace that you get below the "Unsatisfied link error," and also by
testing
for changes in behavior when you run Java after deliberately deleting the DLL
or
deliberately renaming some arbitrary text file to be your DLL.

*  The directory containing the DLL ought to be in your PATH.  The recommended
way to
set your path is to change it with the System control panel and open a new
window.
The cygnus.bat file munges the path a little itself, so my own path ends up
looking
like this:

    bash-2.01$ echo $PATH
    /Cygnus/B19/H-i386-cygwin32/bin:/WINNT/system32:/WINNT:/utils:/enscript:
    //E/jdk1.1.5/bin://E/java/HelloWorld

*  I had to hand-edit the DEF file and put in an alias for the exported
function.  Without the alias, it seems that Cygnus is exporting the mangled
name of the function with its offset ("...@8") and Java is looking for an
unmangled name.  My DEF file looks like this:

    bash-2.01$ cat hello.def
    EXPORTS
    Java_HelloWorld_displayHelloWorld=Java_HelloWorld_displayHelloWorld@8
    dll_entry@12

Please let me know if any of this has been helpful.

--Andrew Mickish
  [email-masked]

--------------ABE44DF5F82EB94F222D734B
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Date: Thu, 11 Jun 1998 19:48:11 -0400
From: Andrew Mickish <[email-masked]>
To: [email-masked]
Subject: Java JNI with GNU-WIN32 gcc
Sender: [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".

--------------ABE44DF5F82EB94F222D734B--

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