This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Java JNI with GNU-WIN32 gcc


FAQ Maintainers:  Please add this info to your sites.


_________________________________________________________________

Subject: More Java JNI with GNU-WIN32 gcc
   Date: Tue, 30 Jun 1998 14:05:03 -0400
   From: Andrew Mickish <mickish@CMU.EDU>
     To: gnu-win32@cygnus.com

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@8dll_entry@12


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

--Andrew Mickish
  mickish@cmu.edu

__________________________________________________________________

Subject: Java JNI with GNU-WIN32 gcc
   Date: Thu, 11 Jun 1998 19:48:11 -0400
   From: Andrew Mickish <mickish@CMU.EDU>
     To: gnu-win32@cygnus.com

> 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
>   mickish@cmu.edu
>


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
  mickish@cmu.edu


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]