java jni linux_java jni实现linux环境下绑定硬件的License
由于系統(tǒng)運(yùn)行在Linux環(huán)境中,該License綁定服務(wù)器的cpuid和mac等信息,而java實(shí)現(xiàn)起來(lái)不太方便所以就利用了JNI
問(wèn)題及解決方法:
1、System.loadLibrary("License");時(shí)出錯(cuò)
解決:libLicense.so文件要放到正確的目錄下,可以利用System.getProperty("java.library.path"));打印出path,我放的是 /lib/ 下面
2、verifylicense 本地方法找不到
解決:頭文件和C的JNIEXPORT 定義的方法名要正確,包含License.java的包名,如果Java中定義的method有劃線的話,頭文件中下劃線后面要緊跟一個(gè)1
3、加載文件和方法都沒(méi)有問(wèn)題,運(yùn)行的時(shí)候沒(méi)有執(zhí)行
解決:這個(gè)問(wèn)題困擾了我一段時(shí)間,最后發(fā)現(xiàn)安裝環(huán)境時(shí)加了一個(gè)文件:/etc/profile,而這個(gè)文件里面沒(méi)有聲明CATALINA_HOME、CATALINA_BASE,并且CLASSPATH里面沒(méi)有帶上servlet-api.jar包,所以native方法無(wú)法在servlet容器中執(zhí)行
部分代碼:
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 [版本號(hào), 2013-7-5]
* @see [相關(guān)類/方法]
* @since [產(chǎn)品/模塊版本]
*/
public class License extends HttpServlet
{
/**
* 注釋內(nèi)容
*/
private static final long serialVersionUID = 1L;
/**
* 日志對(duì)象
*/
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
總結(jié)
以上是生活随笔為你收集整理的java jni linux_java jni实现linux环境下绑定硬件的License的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 颐和园优惠门票政策
- 下一篇: 摩尔庄园暗夜喵喵车多少钱