Re: 64bit (g)fortran JNI
On 4/12/2011 7:25 PM, Jerry DeLisle wrote: On 04/12/2011 07:38 AM, Hans Horn wrote: Folks, has anybody got any experience interfacing (g)fortran routines with Java via JNI? I'm on 64bit Windows7 using cygwin x86_64-w64-mingw32-gcc and x86_64-w64-mingw32-gfortran, both v4.5.2 Java: jdk-6u24-windows-x64 Even though I can statically link the JNI code successfully (using gfortran as linker), calling any native method from Java crashes the JVM - even when no fortran code is called at all. What do you mean by native method? Are you trying to call Fortran procedures from Java? In what way does it crash? Can you get a back trace from a debugger? Jerry PS I have no experience with this, just asking some questions to glean additional information. Fortran calling conventions are no exactly the same as C. Ok, > What do you mean by native method? Are you trying to call Fortran > procedures from Java? Nope, via a C wrapper. I condensed things to the simplest possible testcase that still does not function. In the process I figured that I needed to link with '-shared'. Without that I get said crash of the JVM in both cases, with or without calling fortran. here's my fortran routine: subroutine f77sub(string) character*(*) string write(*,*) 'string is ',string call fflush(6) end here's the JNI C wrapper that calls the fortran routine: #include "JNItest.h" #ifdef F77SUB void f77sub(char*, int); #endif JNIEXPORT void JNICALL Java_JNItest_java2C2f77 (JNIEnv* env, jclass obj) { #ifdef F77SUB printf("calling fortran\n"); fflush(stdout); f77sub("here is fortran", 15); printf("back from fortran\n"); fflush(stdout); #else printf("not calling fortran at all\n"); fflush(stdout); #endif } here's my Java class calling the JNI C wrapper: class JNItest { private native void java2C2f77 (); static { System.loadLibrary("jnitest"); } public static void main (String[] args) { new JNItest().java2C2f77(); } } here's the makefile used to compile the jnitest.dll: gcc_opt = -O3 -std=c99 -DCYGWIN -Wl,--kill-at \ -fno-omit-frame-pointer -I${JAVA_HOME}/include/win32 \ -I${JAVA_HOME}/include -Wall -D_JNI_IMPLEMENTATION_ gcc = gcc gfc = gfortran gfc_opt = -O3 -fno-underscoring -fno-f2c -W -Wunused \ -Wuninitialized -fno-omit-frame-pointer ld_opt = -luuid -lole32 -fno-omit-frame-pointer -shared ifeq ($(PROCESSOR_ARCHITEW6432), AMD64) #use mingw 64bit cross compilers gcc_opt += -m64 gcc = x86_64-w64-mingw32-gcc gfc = x86_64-w64-mingw32-gfortran gfc_opt += -m64 ld_opt += -m64 endif allnof: jnitest_h jnitestnof; jnitest_h: JNItest.class; javah -classpath ./ JNItest jnitestnof: JNItest.c JNItest.h; \ ${gcc} ${gcc_opt} JNItest.c ${ld_opt} -o jnitest.dll # attempt to call fortran from JNI code; must link with gfortran else # we're mssing fortran runtime libs, eg. _gfortran_st_write allf: f77o jnitest_h jnitestf; f77o: f77sub.o; ${gfc} ${gfc_opt} -c f77sub.f -o f77sub.o jnitestf: JNItest.c JNItest.h; \ ${gcc} -DF77SUB ${gcc_opt} -c JNItest.c -o JNItest.o; \ ${gfc} ${ld_opt} JNItest.o f77sub.o -o jnitest.dll -- the dll built w/o calling fortran (-DF77SUB not set; make jnitestnof) prints (as it should): "not calling fortran at all" --- the dll built with calling fortran (-DF77SUB set; make jnitestf, gfortran as linker) is expected to print: calling fortran here is fortran back from fortran however, it produces an unsatisfied link error such as: java.lang.UnsatisfiedLinkError: D:\native\jnitest.dll: %1 is not a valid Win32 application at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1028) at JNItest.(JNItest.java:4) Note that the path to the gfortran runtime is included in the Windows PATH variable: PATH=D:\native;C:\CygWin\usr\i686-w64-mingw32\sys-root\mingw\bin;... Thx., H. -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
On Wed, Apr 13, 2011 at 4:25 AM, Jerry DeLisle wrote: (snip) > Fortran calling conventions are not exactly the same as C. And "The __fortran calling convention isn't the calling convention used by FORTRAN" :) http://blogs.msdn.com/b/oldnewthing/archive/2010/12/22/10108152.aspx Csaba -- GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++ The Tao of math: The numbers you can count are not the real numbers. Life is complex, with real and imaginary parts. "Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds "People disagree with me. I just ignore them." -- Linus Torvalds -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
On 04/12/2011 07:38 AM, Hans Horn wrote: Folks, has anybody got any experience interfacing (g)fortran routines with Java via JNI? I'm on 64bit Windows7 using cygwin x86_64-w64-mingw32-gcc and x86_64-w64-mingw32-gfortran, both v4.5.2 Java: jdk-6u24-windows-x64 Even though I can statically link the JNI code successfully (using gfortran as linker), calling any native method from Java crashes the JVM - even when no fortran code is called at all. What do you mean by native method? Are you trying to call Fortran procedures from Java? In what way does it crash? Can you get a back trace from a debugger? Jerry PS I have no experience with this, just asking some questions to glean additional information. Fortran calling conventions are no exactly the same as C. -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
On 4/12/2011 11:10 AM, Hans Horn wrote: On 4/12/2011 8:02 AM, Eliot Moss wrote: For x64 Windows releated target questions it would be better if you are posting to mingw-w64's ML: I redirect this thread to this list. Elliot, [Eliot] I've been using the cygwin gcc compiler (with the -mno-cygwin option) for years to build JNI dlls (interfacing with C code) that ran fine under 32bit JVMs on windows32. After migrating to 64bit windows7 last year, I've been using x86_64-w64-mingw32-gcc to do the same, ie. build JNI dlls that run fine under 64bit JVMs on windows64. However, throwing fortran in the mix is causing the trouble I'm seeking help for. I see; I appreciate knowing that. Of course, though you're using cygwin to run the compiler, it's really a cross compile to the Windows environment, not *targeting* cygwin. But I can see why it might be at least a little relevant to this list. Sorry -- EM -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
On 4/12/2011 7:48 AM, Kai Tietz wrote: 2011/4/12 Hans Horn: Folks, has anybody got any experience interfacing (g)fortran routines with Java via JNI? I'm on 64bit Windows7 using cygwin x86_64-w64-mingw32-gcc and x86_64-w64-mingw32-gfortran, both v4.5.2 Java: jdk-6u24-windows-x64 Even though I can statically link the JNI code successfully (using gfortran as linker), calling any native method from Java crashes the JVM - even when no fortran code is called at all. my compiler/linker flags are: gcc_opt = -O3 -std=c99 -DCYGWIN -Wl,--kill-at -mno-cygwin -shared -I${JAVA_HOME}/include/win32 -m64 gfc_opt = -O3 -fno-underscoring -fno-f2c -W -Wunused -Wuninitialized -m64 ld_opt = -luuid -lole32 -m64 Any clues? Thx., H. P.S. I'd posted this on the gfortran list, but the chaps there were too busy. Hans, this issues you see here might be reasoned by the gcc version you are using. There is no SEH unwinding information generated for 4.5.2 gcc. This feature was added to gcc 4.6.0 version for windows x64 target. But it might help to use here -fno-omit-frame-pointer option. At least this helped me once to call JNI via Obj-C in a working way. For x64 Windows releated target questions it would be better if you are posting to mingw-w64's ML: I redirect this thread to this list. Regards, Kai Kai, adding -fno-omit-frame-pointer to the gcc, gfc and ld options did not affect the JVM crashes. Thx., H. -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
On 4/12/2011 8:02 AM, Eliot Moss wrote: For x64 Windows releated target questions it would be better if you are posting to mingw-w64's ML: I redirect this thread to this list. Yes, definitely: - cygwin is for 32-bit only - cygwin is not really compatible with even a 32-bit JVM if you were thinking of using cygwin to run JNI code. The JNI system in Windows JVMs is built for Windows, not cygwin. (In principle one could build a JVM using cygwin, but the most popular ones are not built that way, and in fact I don't know of a full-strength JVM (any really) built for/with cygwin.) Best -- Eliot Moss Elliot, I've been using the cygwin gcc compiler (with the -mno-cygwin option) for years to build JNI dlls (interfacing with C code) that ran fine under 32bit JVMs on windows32. After migrating to 64bit windows7 last year, I've been using x86_64-w64-mingw32-gcc to do the same, ie. build JNI dlls that run fine under 64bit JVMs on windows64. However, throwing fortran in the mix is causing the trouble I'm seeking help for. H. -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
For x64 Windows releated target questions it would be better if you are posting to mingw-w64's ML: I redirect this thread to this list. Yes, definitely: - cygwin is for 32-bit only - cygwin is not really compatible with even a 32-bit JVM if you were thinking of using cygwin to run JNI code. The JNI system in Windows JVMs is built for Windows, not cygwin. (In principle one could build a JVM using cygwin, but the most popular ones are not built that way, and in fact I don't know of a full-strength JVM (any really) built for/with cygwin.) Best -- Eliot Moss -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Re: 64bit (g)fortran JNI
2011/4/12 Hans Horn : > Folks, > > has anybody got any experience interfacing (g)fortran routines with Java via > JNI? > > I'm on 64bit Windows7 using cygwin > x86_64-w64-mingw32-gcc and x86_64-w64-mingw32-gfortran, both v4.5.2 > > Java: jdk-6u24-windows-x64 > > Even though I can statically link the JNI code successfully (using gfortran > as linker), calling any native method from Java crashes the JVM - even when > no fortran code is called at all. > > my compiler/linker flags are: > > gcc_opt = -O3 -std=c99 -DCYGWIN -Wl,--kill-at -mno-cygwin -shared > -I${JAVA_HOME}/include/win32 -m64 > > gfc_opt = -O3 -fno-underscoring -fno-f2c -W -Wunused -Wuninitialized -m64 > > ld_opt = -luuid -lole32 -m64 > > > Any clues? > > Thx., > H. > > > P.S. I'd posted this on the gfortran list, but the chaps there were too > busy. Hans, this issues you see here might be reasoned by the gcc version you are using. There is no SEH unwinding information generated for 4.5.2 gcc. This feature was added to gcc 4.6.0 version for windows x64 target. But it might help to use here -fno-omit-frame-pointer option. At least this helped me once to call JNI via Obj-C in a working way. For x64 Windows releated target questions it would be better if you are posting to mingw-w64's ML: I redirect this thread to this list. Regards, Kai -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple