CygWin gcc - Instanting DLL

Tried to test gcc with dll to get something simple going. Far from it!! :)

The problem is that it instantes the dll, because if I change the dll it would report:

java.lang.UnsatisfiedLinkError: no whateverNative in java.library.path

So I know it loads but it doesn't run a simple print function. Heres the source code below and the commands I've used.

HelloNative.java

============

public class HelloNative {

static {

System.loadLibrary("HelloNative");

}

public native static void greeting();

}

HelloNativeTest.java

===============

public class HelloNativeTest {

public static void main(String args[]) {

HelloNative.greeting();

}

}

-

Ran javah HelloNative to produce HelloNative.h

-

HelloNative.c

==========

#include "HelloNative.h"

#include "foo.h"

#include <stdio.h>

#define STRICT

JNIEXPORT void JNICALL Java_HelloNative_greeting (JNIEnv* env, jclass c1)

{

// Want to print via native greeting call.

// It doesn't seem to call it?

printf("Hello!");

}

foo.h

====

#if defined(MAKEDLL)

# define INTERFACE __declspec(dllexport)

#endif

-

With include foo.h or not, same problem

-

Change my JAVA_HOME\include\win32\jni_md.h as instructed from the cygwin to:

jni_md.h

======

#ifndef _JAVASOFT_JNI_MD_H_

#define _JAVASOFT_JNI_MD_H_

#define JNIEXPORT __declspec(dllexport)

#define JNIIMPORT __declspec(dllimport)

#define JNICALL __stdcall

typedef long jint;

#ifdef __GNUC__

typedef long long jlong;

#else

typedef __int64 jlong;

#endif

typedef signed char jbyte;

#endif /* !_JAVASOFT_JNI_MD_H_ */

-

This is so I don't get those could not parse jlong errors.

Seems find so far, now to compile and build the dll

-

gcc -DMAKEDLL -c -I$JAVA_HOME/include -I$JAVA_HOME/include/win32 HelloNative.c -o HelloNative.o

No compile errors, HelloNative.o created.

This is the hard part, creating the dll.

I did:

gcc -shared -o HelloNative.dll HelloNative.o

Tried also:

gcc -Wl,--out-implib,libHelloNative.import.a -shared -o HelloNative.dll HelloNative.o

Ran java HelloNativeTest and I get no output?

It loads the dll, but doesn't instantiate it. If anyone knows a way to get it fully linked, let me know.

Thanks

Abraham Khalil

[2664 byte] By [akhalil100] at [2007-9-26 10:29:06]
# 1

Heres a deeper test. I also put a getGreeting method in c, to return the string back to Java to see more in depth

what the problem is.

HelloNative.java

==============

public class HelloNative {

static {

System.loadLibrary("HelloNative");

}

public native static void greeting();

public native static String getGreeting();

}

HelloNative.c

===========

#include "HelloNative.h"

#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloNative_greeting (JNIEnv* env, jclass c1)

{

printf("Hello!");

fflush(stdout);

)

JNIEXPORT jstring JNICALL Java_HelloNative_getGreeting(JNIEnv* env, jclass c1) {

jstring jstr;

char greeting[] = "Hello, Native World\n";

jstr = (*env) -> NewStringUTF(env, greeting);

return jstr;

}

HelloNativeTest

============

public class HelloNativeTest {

public static void main(String args[]) {

System.out.println("Start of main...");

HelloNative.greeting();

System.out.println("Pass print greeting...");

String mesg = HelloNative.getGreeting();

System.out.println("Return message => " + mesg);

}

}

HelloNativeTest main class prints "Start of main...", but won't print the next message.

In other words its going to the native method, loading the dll, maybe calling the C method and NOT

returning back to the Java program.

If I comment HelloNative.greeting() and want to test HelloNative.getGreeting(), ie return the string from C back to Java. It won't get to the "Return message => " The class HelloNativeTest doesn't freeze and exits without printing a test message, after making any of the native calls.

akhalil100 at 2007-7-1 22:46:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

FOUND A SOLUTION!! :)

When to http://www.xraylith.wisc.edu/~khan/software/gnu-win32/README.dllhelpers-0.2.5.txt

Under point numver 6, it shows how to use JNI with Java under gcc/cygwin32

I was building the dll incorrectly.

Used the commands from their tutorial as:

gcc -g -O2 -c -I$JAVA_HOME/include -I$JAVA_HOME/include/win32 -g HelloNative.c

dllwrap --output-def HelloNative.def --add-stdcall-alias -o HelloNative.dll -s HelloNative.o

THEIR IS STILL A PROBLEM!! Even tho its works under cygwin.

When you try to run in DOS, ie outside cygwin, you have to put the cygwin1.dll in that directory.

It outputs but NOT EXACTLY as if you working in cygwin.

Cygwin makes the two native calls in my last posting properly and prints the result.

On DOS, it makes one native call, and print the result and thats it!?!?!?!

Imagine you have a java application thats needs to make native calls and you NEED to install cygwin with

gcc to get it working. Quite ridiculous. Their must be a way for cygwin to compile it to a dll and use it outside

cygwin?

akhalil100 at 2007-7-1 22:46:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Sorry, wrong URL :) http://www.xraylith.wisc.edu/~khan/software/gnu-win32/
akhalil100 at 2007-7-1 22:46:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

i have made the following makefile that can be run inside cygwin or in dos as long as you copy the following into system32:

gcc.exe

make.exe

as.exe

dllwrap.exe

cygwin1.dll

The trouble is that it doesnt quite work! when it gets to the gcc line it lists about 50 errors to do with jni.h which is in the java folder.

then the java wont work either :-(

any ideas?

######################################################################

# Choose your favorite C compiler

CC = gcc

FILENAME = HelloNative

JAVA_HOME = C:/Program\ Files/j2sdk1.4.2

######################################################################

java:

javac $(FILENAME).java

header:java

javah -classpath . $(FILENAME)

c:java header

$(CC) -g -O2 -c -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/win32 -g $(FILENAME).c

dllwrap --output-def $(FILENAME).def --add-stdcall-alias -o $(FILENAME).dll -s $(FILENAME).o

clean:

\rm -f *.o *.a

kevinherring at 2007-7-1 22:46:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
see a full sample: http://forum.java.sun.com/thread.jsp?forum=52&thread=476416&message=2412131#2412131
michbau at 2007-7-1 22:46:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...