简体   繁体   English

JNI(Java和C ++)在Ubuntu 11.10上使用Eclipse

[英]JNI (Java and C++) Using Eclipse on Ubuntu 11.10

Hi I'm new to JNI and am just trying to get a simple JNI example to work using Eclipse. 嗨,我是JNI的新手,只是想获得一个简单的JNI示例以使用Eclipse进行工作。 I followed the steps in the following tutorial: http://codeandme.blogspot.com/2011/09/jni-made-easy.html My Java code was as follows: 我按照以下教程中的步骤进行操作: http : //codeandme.blogspot.com/2011/09/jni-made-easy.html我的Java代码如下:

public class Main 
{
    private native int getDouble(int n);
    public static void main(String[] args)
    {
        System.load("/home/ryan/Desktop/libJNILibrary3.so");
        Main m = new Main();
        System.out.println(m.getDouble(3));
    }
 }

I then used the javah tool contained in jdk1.7.0_11/bin to generate a header file. 然后,我使用jdk1.7.0_11 / bin中包含的javah工具来生成头文件。

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Main */

#ifndef _Included_Main
#define _Included_Main
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Main
 * Method:    getDouble
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_Main_getDouble
  (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

I then created a C++ project in eclipse and created a Main.cpp source file. 然后,我在eclipse中创建了一个C ++项目并创建了Main.cpp源文件。 I placed the Main.h file in the same folder as Main.cpp. 我将Main.h文件放置在与Main.cpp相同的文件夹中。 The code for Main.cpp was as follows: Main.cpp的代码如下:

#include "Main.h"

JNIEXPORT jint JNICALL Java_Main_getDouble
  (JNIEnv *env, jobject obj, jint n)
{
    return n*2;
}

I then followed the instructions in the tutorial for configuring the project build settings. 然后,我按照教程中的说明配置项目构建设置。 When I built the project, it created the ".so" file "libJNILibrary3.so". 当我构建项目时,它创建了“ .so”文件“ libJNILibrary3.so”。 I then ran my Java class "Main" and received the following error: 然后,我运行了Java类“ Main”,并收到以下错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Main.getDouble(I)I
    at Main.getDouble(Native Method)
    at Main.main(Main.java:9)

The strange thing is, if I redo this example, and make it so the getDouble method takes no arguments but merely returns the value 2, then everything works how it should. 奇怪的是,如果我重做此示例,并使其如此,则getDouble方法不接受任何参数,而仅返回值2,那么一切都会按预期进行。 Making the method take arguments is somehow messing everything up. 使方法带有参数会以某种方式弄乱一切。 I have spent over ten hours on this error and have searched many forums, but have not found a solution to this problem. 我已经花了十多个小时解决此错误,并搜索了许多论坛,但没有找到解决此问题的方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

You placed the body of your function in a cpp file and the JNI callback has to follow C style name mangling. 您将函数的主体放置在cpp文件中,并且JNI回调必须遵循C样式名称修饰。

try this in your cpp file: 在您的cpp文件中尝试一下:

extern "C"
{

JNIEXPORT jint JNICALL Java_Main_getDouble
  (JNIEnv *env, jobject obj, jint n)
{
    return n*2;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM