码迷,mamicode.com
首页 > 编程语言 > 详细

java通过jna调用so

时间:2019-10-16 20:16:25      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:color   Once   eva   package   extend   public   span   ica   jar   

c++:

FirstEliteValidate.h

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

    int FirstEliteValidateTest();

#ifdef __cplusplus
}
#endif

FirstEliteValidate.cpp

#include "FirstEliteValidate.h"

/*
    To test the library, include "FirstEliteValidate.h" from an application project
    and call FirstEliteValidateTest().
    
    Do not forget to add the library to Project Dependencies in Visual Studio.
*/

static int s_Test = 0;

int FirstEliteValidateTest()
{
    return ++s_Test;
}

编译出so文件命名为 libFirstEliteValidate.so,复制到 /usr/lib下

java:

package com.bg.security;

import java.util.Properties;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class Program4Linux {
    
    public interface CLibrary extends Library {
        int FirstEliteValidateTest();
    }

    
    public static void main(String[] args) 
    {
        CLibrary lib = (CLibrary) Native.load("FirstEliteValidate", CLibrary.class);
        int a = lib.FirstEliteValidateTest();
        int b = lib.FirstEliteValidateTest();
        int c = lib.FirstEliteValidateTest();

        System.out.println("a=" + a);
        System.out.println("b=" + b);
        System.out.println("c=" + c);
        
        Properties props = System.getProperties();
        System.out.println("Operation System:" + props.getProperty("os.name"));
        System.out.println("System Version:" + props.getProperty("os.version"));
        System.out.println("end");
    }
}

 运行:

java -Djava.ext.dirs=/home/knife/DemoCode/jar com.bg.security.Program4Linux

 

java通过jna调用so

标签:color   Once   eva   package   extend   public   span   ica   jar   

原文地址:https://www.cnblogs.com/nanfei/p/11687620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!