java使用JNI调用驱动-学习笔记

这篇博客详细记录了如何使用Java的JNI技术调用C语言编写的驱动程序。首先介绍了文件结构,包括TestJni.java(Java程序)、native.c(JNI程序)、tty.c(驱动程序)和Makefile。接着,作者列出了一步一步的操作流程:编译Java源代码生成class文件,使用javah生成JNI头文件,然后用gcc创建动态链接库libnavite.so,并将其复制到系统库中。通过这个过程,实现了Java程序对块设备的访问功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要实现功能:使用Jni调用c语言,访问块设备

文件描述:
  1. TestJni.java 是java程序
  2. navite.c 是jni程序(java调用c语言)
  3. tty.c 驱动程序
  4. Makefile 通用makefile
操作步骤:

TestJni.java 和 navite.c在一个文件夹里 ,在这个目录下执行以下操作:
1. 执行javac TestJni.java 生成TestJni.class文件;
2. 执行javah TestJni 生成TestJni.h文件;
3. 执行命令,创建动态链接库:gcc -shared -fPIC navite.c -o libnavite.so -I /usr/local/jdk1.6.0_29/include/ -I /usr/local/jdk1.6.0_29/include/linux
4. 执行命令,把创建在当前文件的动态库拷贝到系统库里:sudo cp lib$a.so /usr/lib


TestJni.java
public class TestJni
{
    static 
    {
        System.loadLibrary("native");
    }

    public native int open(String path);
    public native void close(int i);
    public native String read();
    public native int write();
    public native long ioctl();

    public static void main (String [] args)
    {
        /* code */

        int fs;
        TestJni d = new TestJni();
        System.out.println (fs = d.open("/dev/tty_led"));

        d.write();
        System.out.println(d.read());
        d.close(fs);
    }


}

#include<jni.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>


jint fd;
char buff[128];
jint c_open(JNIEnv *env,jobject obj,jstring prompt)
{
    system("sudo chmod 777 /dev/tty_led");
    const char *str;
    str = (*env)->GetStringUTFChars(env, prompt, NULL);
    if (str == NULL) {
        return 1; /* OutOfMemoryError already thrown */
    }

    fd = open(str ,O_RDWR);
    if(fd < 0)
    {
        perror("open");
        return 1;
    }

    printf("c_open =%s \n",str);
    (*env)->ReleaseStringUTFChars(env, prompt, str);
    return fd;
}


void c_close(JNIEnv *env,jobject obj,jint fs)
{

    close(fs);
    printf("c_close \n");
    return ;
}


jstring c_read(JNIEnv *env,jobject obj)
{
    jint rdnm;
    rdnm = read(fd,buff,sizeof(buff));
    if(rdnm == -1)
    {
        perror("read");

    }
    printf("c_read = %s \n",buff);

    return (*env) -> NewStringUTF(env,buff);
}

jint c_write(JNIEnv *env,jobject obj)
{
    jint wdnm;
    wdnm = write(fd,"hello_me",9);
    if(wdnm == -1)
    {
        perror("write");
        return 1;
    }

    printf("c_write \n");
    return wdnm;
}

static  JNINativeMethod  method[] ={
    "open","(Ljava/lang/String;)I",(void *)c_open,
    "close","(I)V",(void *)c_close,
    "read","()Ljava/lang/String;",(void *)c_read,
    "write","()I",(void *)c_write,
};

JNIEXPORT JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
    printf("***********************\n");
    JNIEnv *env;
    jclass cls;

    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
        return JNI_ERR; /*获得jvm环境*/
    }
    cls = (*env)->FindClass(env, "TestJni");/*找类*/
    if (cls == NULL) {
        return JNI_ERR;
    }
    (*env)->RegisterNatives(env, cls, method, 4);/*注册*/
    return JNI_VERSION_1_2;
}

tty.c
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<asm/io.h>
#include<asm/uaccess.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/init.h>

MODULE_LICENSE("GPL");
#define TTY_MAJOR 500
#define TTY_MINOR 0

static dev_t from;

static struct cdev tty_cdev;
static struct class *mtty;

static char buff[]={"jjjkkk"};
static ssize_t tty_read (struct file *filp, char __user *user, size_t size, loff_t *loff)
{
     copy_to_user(user, buff, sizeof(buff));
    printk("tty_read \n");
    return 0;
}

ssize_t tty_write (struct file *filp, const char __user *user, size_t size, loff_t *loff)
{
    printk("tty_write \n");
    return 0;
}

long tty_unlocked_ioctl (struct file *filp, unsigned int cmd, unsigned long num)
{
    printk("tty_unlocked_ioctl \n");
    return 0;
}

int tty_open (struct inode *inode, struct file *filp)
{

    printk("tty_open \n");
    return 0;
}
int tty_release (struct inode *inode, struct file *filp)
{

    printk("tty_release \n");
    return 0;
}






struct file_operations fopes =  {
    .owner = THIS_MODULE,
    .read = tty_read,
    .write= tty_write,
    .unlocked_ioctl = tty_unlocked_ioctl,   
    .open= tty_open,
    .release=tty_release,

};



static int tty_init(void)
{
    from = MKDEV(TTY_MAJOR,TTY_MINOR);
    register_chrdev_region(from,1,"tty_led");
    cdev_init(&tty_cdev,&fopes);
    tty_cdev.owner = THIS_MODULE;
    cdev_add(&tty_cdev,from,1);

    mtty = class_create(THIS_MODULE,"tty_led");
    device_create(mtty,NULL,from,NULL,"tty_led");

    printk("tty_init \n");

}

static void tty_exit(void)
{

    device_destroy(mtty,from);
    class_destroy(mtty);
    cdev_del(&tty_cdev);
    unregister_chrdev_region( from,1);

    printk("tty_exit \n");
}

module_init(tty_init);
module_exit(tty_exit);

Makefile
$(warning KERNELRELEASE=$(KERNELRELEASE))
ifeq ($(KERNELRELEASE),)

#KERNELDIR ?= /home/lht/kernel2.6/linux-2.6.14

KERNELDIR ?= /lib/modules/$(shell uname -r)/build  
PWD := $(shell pwd)

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*

.PHONY: modules modules_install clean

else
    obj-m := tty.o
endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值