Search code examples
java-native-interfacecygwin

Using Java / JNI on Cygwin


I am entering into Java world just now. I am facing problem when I try to compile sample program for Calling C from Java.

I wanted to try the sample code for JNI (Which is given in JNI.PDF from SUN-Micro).

The code is some thing like this:

class HelloWorld {
  private native void print();
  public static void main(String[] args) {
    new HelloWorld().print();
  }
  static {
    System.loadLibrary("HelloWorld");
  }
}

I compiled using javac HelloWorld.java and after that got .H file for C program using "javah -jni HelloWorld"

Even my C code is also simple with :

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
  printf("Hello World!\n");
  return;
}

I created .dll in Cygwin using "gcc -Wl,--add-stdcall-alias -shared -o HelloWorld.dll HelloWorld.c"

Even I used normal Linux method of Creating Shared Lib: "gcc -shared -Wl,-soname,libHelloWorld.so -o libHelloWorld.so HelloWorld.o"

But when I run java HelloWorld, it return Aborted!

Since i dont have much idea in Java, I am not sure where I going wrong.

I checked similar code present in few websites like: http://www.inonit.com/cygwin/jni/helloWorld/ but getting similar error.


Solution

  • The InOnIt page uses -mno-cygwin. If you want Cygwin functionality, then you'll want http://elliotth.blogspot.com/2005/08/porting-jni-code-to-win32-with-cygwin.html. Terminator's moved since that was written. The crux of the source you'd want for a Cygwin-safe Java launcher is now at https://github.com/software-jessies-org/jessies/blob/master/salma-hayek/native/all/java-launcher/java-launcher.cpp.