先用eclipse 创建 Java Project;
然后直接在项目中添加Prompt.java文件,放在default package下(最好不要添加包,否则容易出错)。
 
1. 编写Java文件,在其中声明native方法, 并通过static 语句块加载动态链接库,示例Prompt.java代码如下:
class Prompt {
    private native String getLine(String prompt);
 
    public static void main(String args[]) {
        Prompt p = new Prompt();
        String input = p.getLine("Type a line: ");
        System.out.println("User typed: " + input);
    }
 
    static {
        System.loadLibrary("Prompt");  //这里到so库名千万别搞错了, Prompt对应的实际库名称是libPrompt.so
    }
}
 
2.调用javac命令生成Prompt.class文件;
javac Prompt.java
3.调用javah命令生成Prompt.h头文件供C程序引用:
javah -jni Prompt
自动生成的头文件如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Prompt */
 
#ifndef _Included_Prompt
#define _Included_Prompt
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Prompt
 * Method:    getLine
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Prompt_getLine
  (JNIEnv *, jobject, jstring);
 
#ifdef __cplusplus
}
#endif
#endif
4.编写Prompt.c文件实现具体功能:
#include <jni.h>
#include <stdio.h>
#include "Prompt.h"
 
JNIEXPORT void JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt) 
{
    char buf[128];
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, prompt, NULL);
    if(str == NULL) {
        return NULL;        
    }
    printf("%s", str);
    (*env)->ReleaseStringUTFChars(env, prompt, str);
    scanf("%s", buf);
    return (*env)->NewStringUTF(env, buf);
}
5. 编译动态库libPrompt.so;
可能会报如下错误:

/usr/bin/ld: /tmp/ccG1IYKj.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccG1IYKj.o: error adding symbols: 错误的值
collect2: error: ld returned 1 exit status

加上编译选项-fPIC即可;
gcc -shared -fpic -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux Prompt.c -o libPrompt.so
(gcc -shared -fPIC -I /opt/Java/jdk1.8.0_71/include/ -I /opt/Java/jdk1.8.0_71/include/linux/ Prompt.c -o libPrompt.so)
 
6. 运行。
java Prompt
可能会报如下错误

Exception in thread "main" java.lang.UnsatisfiedLinkError: no test in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Test.<clinit>(Test.java:14)

需要设置java.library.path的路径来执行:

java -Djava.library.path=. Prompt

[转]Ubuntu下使用Jni开发例子的更多相关文章

  1. Ubuntu下的PHP开发环境架设

    Ubuntu下的PHP开发环境架设   今天重新装了ubuntu那么就吧过程记录下. 打开终端,也就是命令提示符. 我们先来最小化组建安装,按照自己的需求一步一步装其他扩展.命令提示符输入如下命令: ...

  2. ubuntu下安装 openssl 开发库

    ubuntu下安装 openssl 开发库 检查是否已安装openssl: sudo apt-get install openssl 如果已安装执行以下操作:sudo apt-get install ...

  3. ubuntu下搭建JAVA开发环境【转】

    转自:http://jingyan.baidu.com/article/86fae346b696633c49121a30.html JAVA开发环境是一种跨平台的程序设计语言,可以在windows.L ...

  4. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  5. ubuntu下安装stm32开发环境

    在windowns下开发stm32刚开始学最烦的就是创建工程模板,都不知道为什么要那样设置,而且步骤繁多.现在我告诉大家一个好消息,在linux下配置stm32开发环境包括创建工程,使用JLink仿真 ...

  6. ubuntu下搭建android开发环境之超顺畅模拟器

    如果说android系统的卡,像耳边蚊子让人抓狂,那么android模拟器的卡,那就像午睡时的苍蝇.大概就是一样的恶心~~ 那么,这样的问题对于开发者肯定忍无可忍,我也一样,虽然我还没有入门,但我也一 ...

  7. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  8. 【android 开 发 】 - Android studio 下 NDK Jni 开发 简单例子

    Android 开发了一段时间,一方面 ,感觉不留下点什么.有点对不起自己, 另一方面,好记性不如烂笔头,为了往后可以回头来看看,就当做是笔记,便决定开始写博客.废话不多说 ! 今天想搞一搞 ndk ...

  9. Android studio 下 NDK Jni 开发 简单例子

    1. 创建一个新的工程 2. 创建一个新的类 JniText.java  点击Build--Make Project  后     选中工程 点击F4键 sdk location 中 Android ...

随机推荐

  1. Codeforces Round #345 (Div. 2)

    DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h> typedef long ...

  2. Windows RC版、RTM版、OEM版、RTL版、VOL版的区别

    Windows 版本号标识区别一览表: 版本缩写 版本全称 版本意义 Alpha版 Alpha 内部测试版,一般不会向外部发布,会有很多Bug,只供测试人员使用,如果您看到Alpha版本了,一般来讲对 ...

  3. 424 - Integer Inquiry

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...

  4. Oracle查询每天固定时间段的数据

    select * from GPS_LOG t where to_char(t.gps_time,'hh24:mm:ss')>='15:30:00'and to_char(t.gps_time, ...

  5. 使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法

    使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法 添加一行 manager. ...

  6. 关于NSNotificationCenter消息通信用法

    NSNotificationCenter主要用于广播消息到多个监听着,其传统用法 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotification ...

  7. [Leetcode] Merge Intevals

    Question: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3], ...

  8. 20145330《Java程序设计》第五次实验报告

    20145330<Java程序设计>第五次实验报告 实验五 Java网络编程及安全 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统 4.结队伙伴 ...

  9. 十、ios 模态窗口[实例]

    一.模态窗口概念 对话框一般分为两种类型:模态类型( modal )与非模态类型( modeless ).所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话 ...

  10. MySQL Command 常见命令

    /* Load data from txt file */ LOAD DATA LOCAL INFILE "D:/data.txt" INTO TABLE tname; /* Lo ...