Using cygwin and JAVA/JNI

William S Fulton wsf@fultondesigns.co.uk
Tue Jan 28 07:34:00 GMT 2003


Following up from recent emails about Java/JNI using Sun's JDK I'd 
thought I'd share my experiences.
Up and until Cygwin-1.3.13 the test script below would compile and run 
as expected. The test produces a simple Java Native Interface (JNI) 
program which calls C code from a Java program using JNI. Releases since 
Cygwin-1.3.13 have resulted in calls to JNI code in a DLL being erratic; 
some Cygwin versions produce a core dump, others result in a non-zero 
program exit code and no error message when calling JNI functions. The 
output below is using Cygwin-1.3.19.
The results are independent of the gcc compiler, I've examined gcc 
2.95-3, gcc-2 2.95.3-10 and gcc 3.2 20020927 and intermediate versions. 
If the dll that these compilers produce is run under Cygwin-1.3.13 and 
earlier versions, the program will work. I've been watching each release 
since then and each produces different ways of failing, but none have 
worked with all the tests I use (SWIG's Java test-suite), unlike pre 
1.3.13. I've used many versions since Cygwin-1.3.3.
If the code is built using -mno-cygwin, the problem also goes away. So 
it looks like it is more recent versions of the Cygwin dll at fault.
If anyone has had any luck with GCJ and JNI on Cygwin I'd appreciate 
info as my experiments and mailing list search has been fruitless.
Cheers
William
------------------- test script: java_cygwin_test ---------------------
Requires JDK to be set to the Sun Java 2 SDK (JDK) installation
-----------------------------------------------------------------------
#! /bin/sh
set -x
#Set path to Sun J2 SDK (JDK)
JDK=c:/j2sdk1.4.1
cat > example.c << 'END'
#if defined(__GNUC__)
 typedef long long __int64; /*For gcc on Windows */
#endif
#include <jni.h>
JNIEXPORT jint JNICALL Java_exampleJNI_timesTwo(JNIEnv *jenv, jclass 
jcls, jint num) {
 return num*2;
}
END
cat > main.java << 'END'
public class main {
 static {
 try {
	System.loadLibrary("example");
 } catch (UnsatisfiedLinkError e) {
 System.err.println("Native code library failed to load.\n" + e);
 System.exit(1);
 }
 }
 public static void main(String argv[]) {
 System.out.println("Java program started");
 int answer = exampleJNI.timesTwo(7);
 System.out.println( "7*2 = " + answer);
 }
}
END
cat > exampleJNI.java << 'END'
class exampleJNI {
 public final static native int timesTwo(int num);
}
END
# Recent versions of Cygwin will not work unless using -mno-cygwin
gcc -c -mno-cygwin example.c -I$JDK/include -I$JDK/include/win32
gcc -shared example.o -Wl,--add-stdcall-alias -mno-cygwin -o example.dll
#gcc -c example.c -I$JDK/include -I$JDK/include/win32
#gcc -shared example.o -Wl,--add-stdcall-alias -o example.dll
$JDK/bin/javac *.java
$JDK/bin/java main || echo Java program FAILED
------------------ Results showing working using -mno-cygwin -----
$ ./java_cygwin_test
+ JDK=c:/j2sdk1.4.1
+ cat
+ cat
+ cat
+ gcc -c -mno-cygwin example.c -Ic:/j2sdk1.4.1/include 
-Ic:/j2sdk1.4.1/include/w
in32
+ gcc -shared example.o -Wl,--add-stdcall-alias -mno-cygwin -o example.dll
+ c:/j2sdk1.4.1/bin/javac exampleJNI.java main.java
+ c:/j2sdk1.4.1/bin/java main
Java program started
7*2 = 14
----------- Results showing not working using the Cygwin 1.3.19 DLL
$ ./java_cygwin_test
+ JDK=c:/j2sdk1.4.1
+ cat
+ cat
+ cat
+ gcc -c example.c -Ic:/j2sdk1.4.1/include -Ic:/j2sdk1.4.1/include/win32
+ gcc -shared example.o -Wl,--add-stdcall-alias -o example.dll
+ c:/j2sdk1.4.1/bin/javac exampleJNI.java main.java
+ c:/j2sdk1.4.1/bin/java main
+ echo Java program FAILED
Java program FAILED
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /