java jni linux_java jni实现linux环境下绑定硬件的License

由于系统运行在Linux环境中,该License绑定服务器的cpuid和mac等信息,而java实现起来不太方便所以就利用了JNI

问题及解决方法:

1、System.loadLibrary("License");时出错

解决:libLicense.so文件要放到正确的目录下,可以利用System.getProperty("java.library.path"));打印出path,我放的是 /lib/ 下面

2、verifylicense 本地方法找不到

解决:头文件和C的JNIEXPORT 定义的方法名要正确,包含License.java的包名,如果Java中定义的method有划线的话,头文件中下划线后面要紧跟一个1

3、加载文件和方法都没有问题,运行的时候没有执行

解决:这个问题困扰了我一段时间,最后发现安装环境时加了一个文件:/etc/profile,而这个文件里面没有声明CATALINA_HOME、CATALINA_BASE,并且CLASSPATH里面没有带上servlet-api.jar包,所以native方法无法在servlet容器中执行

部分代码:

1、web.xml

class="java" name="code">

license

com.blingtel.common.util.License

1

2、License.java

package com.xxx.common.util;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/*

* 文件名称: CheckLicense.java

* 文件描述:

* @version 1.0

* @author

*/

/**

*

*

* @author zengyouyuan

* @version [版本号, 2013-7-5]

* @see [相关类/方法]

* @since [产品/模块版本]

*/

public class License extends HttpServlet

{

/**

* 注释内容

*/

private static final long serialVersionUID = 1L;

/**

* 日志对象

*/

private static Log logger = LogFactory.getLog(License.class);

private native boolean verifylicense();

static

{

int i = 0;

try

{

logger.error("loadLibary start...");

System.loadLibrary("License");

i = 1;

logger.error("load Libary OK...");

}

catch (Exception e)

{

i = 2;

// TODO Auto-generated catch block

logger.error("load error");

e.printStackTrace();

Runtime runtime = Runtime.getRuntime();

try

{

i = 3;

logger.error("load libLicense.so error, killall java");

runtime.exec("killall java");

}

catch (Exception e1)

{

i = 4;

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

finally

{

logger.error("i = " + i);

if (i == 0)

{

Runtime runtime = Runtime.getRuntime();

try

{

logger.error("load libLicense.so error, killall java");

runtime.exec("killall java");

System.exit(0);

}

catch (Exception e1)

{

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

}

public void init(ServletConfig servletConfig)

throws ServletException

{

logger.error("income License.init()");

License l = new License();

if (l.verifylicense())

{

logger.error("verify_license ERROR");

Runtime runtime = Runtime.getRuntime();

try

{

logger.error("verify license failed, killall java");

runtime.exec("killall java");

System.exit(0);

}

catch (Exception e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else

{

logger.error("verify license success! create ok.txt file");

logger.error("verify_license OK");

}

}

}

3、License.h

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class License */

#ifndef _Included_License

#define _Included_License

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class: License

* Method: verify_license

* Signature: ()Z

*/

JNIEXPORT jboolean JNICALL Java_com_blingtel_common_util_License_verifylicense

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

4、license_decrypt.c部分代码

#include

#include

#include

#include

#include

#include "base64.h"

#include "rsa.h"

#include

#include "License.h"

#define KEY_LEN 128

#define RSA_N "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"\

"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

#define RSA_E "010001"

#pragma GCC push_options

#pragma GCC optimize ("O0")

int verify_license();

/* Header for class IntArray */

#ifdef __cplusplus

extern "C" {

#endif

JNIEXPORT jboolean JNICALL Java_com_blingtel_common_util_License_verifylicense (JNIEnv * env, jobject job)

{

return verify_license();

}

#ifdef __cplusplus

}

#endif

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380778.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python中abs()和fabs()方法之间的区别

In python, abs() method and fabs() method both are used to find the absolute value of a number. They are used for the same purpose but they have a difference, which we are going to discuss in this tutorial. 在python中, abs()方法和fabs()方法都用于…

php中自动转换、强制转换、其他数据类型和bool转换

0x01 自动转换 运算过程需要的数据类型和提供的数据类型不一致&#xff0c;将数据类型转为自己需要的类型 <?phpheader(content-type:text/html;charsetutf-8);echo 1aa7c;echo <br>; ?>加号做数字运算&#xff0c;会将字符串转为数字 0x02 强制转换 强制将…

tf.acos_带有Python示例的math.acos()方法

tf.acosPython math.acos()方法 (Python math.acos() method) math.acos() method is a library method of math module, it is used to get the arc cosine, it accepts a number between -1 to 1 and returns the arc cosine value (in radians) of the given number. math.a…

新买的锅要怎么处理?-新锅开锅处理

最近很忙&#xff0c;年底刚找了新的住处&#xff0c;刚过完圣诞假就一刻不停地打扫卫生、置办东西。这里天高皇帝远的&#xff0c;行政比较弱&#xff0c;啥东西都要自己买。据说这里出租房子一般连橱柜都不带的&#xff0c;基本上只有墙壁、地板和门。万幸&#xff0c;找到一…

php字符串连接符、三元运算符

字符串连接符&#xff1a;. <?phpheader(content-type:text/html;charsetutf-8);echo my name is. .DL_one; ?>三元运算符 形式&#xff1a;表达式&#xff1f;值1&#xff1a;值2 表达式为true&#xff0c;返回值1&#xff0c;为false&#xff0c;返回值2 <?ph…

java多线程知识_学习知库丨Java多线程知识大全

进程&#xff1a;每个进程都有独立的代码和数据空间(进程上下文)&#xff0c;进程间的切换会有较大的开销&#xff0c;一个进程包含1--n个线程。线程&#xff1a;同一类线程共享代码和数据空间&#xff0c;每个线程有独立的运行栈和程序计数器(PC)&#xff0c;线程切换开销小。…

螺旋遍历_螺旋形式的水平阶遍历

螺旋遍历Problem statement: 问题陈述&#xff1a; Write a program to print Level Order Traversal in spiral form of a binary tree. 编写一个程序以二叉树的螺旋形式打印Level Level Traversal 。 Example: 例&#xff1a; For the above tree:Basic level order trave…

SharePoint2007安装图文详解二:安装AD(活动目录)及DNS

在上一篇SharePoint2007安装图文详解一&#xff1a;安装IIS及相关组件中已经介绍了IIS及相关组件的安装&#xff0c;本篇将详细介绍AD&#xff08;活动目录&#xff09;的安装。 打开“管理您的服务器”&#xff0c;点击“添加或删除角色” 点击“添加或删除角色”后弹出“配置…

php的foreach

作用&#xff1a;遍历数组 索引数组 形式&#xff1a;foreach(数组 as 值){ //操作 } <?phpheader(content-type:text/html;charsetutf-8);$personarray(DL_one,18,man);foreach($person as $chara){echo $chara,<br>;} ?>2. 关联数组 形式&#xff1a;foreach…

strcmp java_C语言中strcmp的实现原型

C语言中strcmp的实现原型实现代码&#xff1a;int __cdecl strcmp (const char * src,const char * dst){int ret 0 ;while( ! (ret *(unsigned char *)src - *(unsigned char *)dst) && *dst)src, dst;if ( ret < 0 )ret -1 ;else if ( ret > 0 )ret 1 ;ret…

带有Python示例的math.sin()方法

Python math.sin()方法 (Python math.sin() method) math.sin() method is a library method of math module, it is used to get the sine of the number in radians, it accepts a number returns the cosine of the given number in radians. math.sin()方法是数学模块的库方…

ExtJs实践(3)——xtype名称与控件对应

xtype可作为Ext控件的简写&#xff0c;都会对应一个Ext控件。当然这里你也可以自定义这个xtype&#xff0c;通过自定义的Ext控件来绑定&#xff0c;主要由Ext.reg方法去注册xtype。Ext.all.js里面包含的xtype包含&#xff1a; xtype Class ------------- -------…

fmax()函数以及C ++中的示例

C fmax()函数 (C fmax() function) fmax() function is a library function of cmath header, it is used to find the maximum value of the given numbers, it accepts two number and returns the larger one. fmax()函数是cmath标头的库函数&#xff0c;用于查找给定数字的…

java date传输类型错误_转换日期格式:Java中的转换错误?

我正在尝试将此日期转换为其他格式。不幸的是&#xff0c;他们成功地解析了日期并正确地保留了所有信息。06-Dec-2017 07&#xff1a;14&#xff1a;56.656PM至2017-12-06 19&#xff1a;14&#xff1a;56.656如果我尝试解析输入日期LocalDateTime.parse("06-Dec-2017 07:…

Linux 2440 LCD 控制器

嵌入式Linux之我行&#xff0c;主要讲述和总结了本人在学习嵌入式linux中的每个步骤。一为总结经验&#xff0c;二希望能给想入门嵌入式Linux的朋友提供方便。如有错误之处&#xff0c;谢请指正。共享资源&#xff0c;欢迎转载&#xff1a;http://hbhuanggang.cublog.cn 一、开…

python 示例_Python中带有示例的关键字除外

python 示例Python关键字除外 (Python except keyword) except is a keyword (case-sensitive) in python, it is used with try... except statement to handle the exception. 除了是python中的一个关键字(区分大小写)&#xff0c;它与try ... except语句一起使用来处理异常。…

java中使用es精准查询_使用ES简单查询语句须知

查询样例{"query": { //1"bool": { ///2"must": [{ //3"query_string": {"default_field": "xxxz","query": " *XXX* "}}],"should": [], //4"must_not": [], 5"fi…

php的create_function、function_exists判断函数是否存在

create_function 格式&#xff1a;create_function(‘参数’,‘函数体代码’) 作用&#xff1a;创建匿名函数 <?phpheader(content-type:text/html;charsetutf-8);$func create_function($a,$b, return ($a$b););echo $func(10,20); ?>function_exists 返回值为boo…

python中dict函数_dict()函数以及Python中的示例

python中dict函数Python dict()函数 (Python dict() function) dict() function is a library function, it is used to create a dictionary with keywords( Ids) and values (s), it accepts a set of values with keywords, values and returns a dictionary. dict()函数是…

最佳ASP.NET编程习惯

初学编程的朋友往往喜欢收集一些很“奇妙”的编程技巧&#xff0c;然而&#xff0c;技巧的积累往往并没有提高程序质量&#xff0c;反而引导一些编程者一味追求奇和新&#xff0c;忘记了基本编程习惯的培养&#xff0c;不利于团队的合作&#xff0c;可能&#xff0c;这也是中国…