This is the mail archive of the mailing list for the Cygwin project. See the Cygwin home page for more information.
Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

JNI C return NaN for float and double


How can I make a JNI interfaced C code, compiled as a dll, return the right
double or float number to its java caller? In my case (see the simple
Mummit's HelloWorld modified example below) it works fine when the returned
type is char or short or long; but it returns a NaN in case of float or double.
I have cygwin-b20.1 and java 1.1.6

Please help!
Thank you
Isselmou Ould Dellahy

============================ The example ===================================

---------------------- -------------------
class HelloWorld {
    public native double displayHelloWorld();

    static {

---------------------- -------------------------

class Main {
    public static void main(String[] args) {
        HelloWorld h = new HelloWorld();
        double d = h.displayHelloWorld();
        System.out.println("zzzzzzzzzz " + d);
---------------------- HelloWorldImp.c -------------------

#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include "HelloWorld.h"

Java_HelloWorld_displayHelloWorld (JNIEnv *env, jobject obj) 
  char *home;
  printf("Java JNI\n");
  home = getenv ("HOME");
  printf("HOME = %s\n", (home) ? home : "(NULL)");
  return 46.8;

---------------------- Makefile --------------------------

# Sample makefile to create Java JNI with Cygwin b20.1 tools. This *will not*
# work with Cygwin versions earlier than b20.1.
# The only difference from creating a regular DLL is to supply a different
# entry point, __cygwin_noncygwin_dll_entry@12, since Java is an MSVC app.

CC = gcc 

DEBUG = -g -Wall
CPPFLAGS = -I. -I$(JDK_ROOT)/include -I$(JDK_ROOT)/include/win32

JDK_ROOT = c:/jdk1.1.6

AS = as
DLLTOOL = dlltool
DLLWRAP = dllwrap

# Various targets to build.
DLL_NAME = hello.dll
DLL_EXP_DEF = hello.def

all: classes $(DLL_NAME)
	jre -cp /users/iod/hifem/etc/classes/ -cp . Main

classes :
	javah HelloWorld

# DLL related variables. These are used when building the DLL. See later.

# Some tools require special CPP macros when building a DLL (eg., _DLL etc).
# Here we don't need anything.


# The default entry point defined by dllwrap is __cygwin_dll_entry@12 
# defined in libcygwin.a, but that's only appropriate for Cygwin apps,
# but since Java is a MSVC app, we need to provide a different entry
# point. Note the leading underscore and the trailing @12.
# The -s flag strips the DLL to shrink the size.

DLL_LDFLAGS = -Wl,-e,__cygwin_noncygwin_dll_entry@12 -s

# any extra libraries that your DLL may depend on.
DLL_LDLIBS = #-luser32

DLL_SRCS  = HelloWorldImp.c
DLL_OBJS  = $(DLL_SRCS:.c=.o)

# Making DLL

DLLWRAP_FLAGS = --output-def $(DLL_EXP_DEF) \
	--add-stdcall-alias \
	--driver-name $(CC) \


# dependencies.

# default rules for building DLL objects. Note that client programs (ie.,
# the ones that *use* the DLL) have to be compiled without the DLL_CFLAGS
# flags.


# Note that we omit the $(DLL_CFLAGS) for client programs.
usedll.o: %o: %c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

	-rm -f $(OBJS) $(DLL_OBJS) $(DLL_NAME) $(DLL_EXP_LIB) $(DLL_EXP_DEF) $(TESTPROGS) *.class *~ HelloWorld.h
---------------------- Makefile end ----------------------

Want to unsubscribe from this list?
Send a message to