在Cygwin上使用Java / JNI [英] Using Java / JNI on Cygwin

查看:321
本文介绍了在Cygwin上使用Java / JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入Java世界。我面临问题,当我尝试编译示例程序从Java调用C。

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

我想尝试JNI的示例代码(这在SUN-Micro的JNI.PDF中给出)。

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

代码是这样的:

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



我使用javac HelloWorld.java编译, .H文件C程序使用javah -jni HelloWorld

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

即使我的C代码也很简单:

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;
}



我在Cygwin中使用gcc -Wl, stdcall-alias -shared -o HelloWorld.dll HelloWorld.c

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

即使我使用正常的Linux创建共享库的方法:gcc -shared -Wl,-soname, libHelloWorld.so -o libHelloWorld.so -WorldWorld.o

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

但是当我运行java HelloWorld时,它返回Aborted!

But when I run java HelloWorld, it return Aborted!

因为我在Java中没有太多的想法,我不知道我在哪里错了。

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

我在几个网站上检查过类似的程式码,例如: http://www.inonit.com/cygwin/jni/helloWorld/ ,但遇到类似错误。

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

推荐答案

InOnIt页面使用-mno-cygwin。如果你想要Cygwin功能,那么你会需要 http://elliotth.blogspot.com/2005/08/porting-jni-code-to-win32-with-cygwin.html 。终结者从那以后被移动了。你想要一个Cygwin安全的Java启动器的源的症结现在在 https://github.com/software-jessies-org/jessies/blob/master/salma-hayek/native/all/java-launcher/ java-launcher.cpp

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.

这篇关于在Cygwin上使用Java / JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆