tomcat在Linux环境下调用so文件执行native方法的问题


<coding-1 lang="xml">
<servlet>
<servlet-name>smcInitServlet</servlet-name>
<servlet-class>com.xxx.common.util.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</coding>

   
  public class InitServlet extends HttpServlet
  
{
/**
* 日志对象
*/
private Log logger = LogFactory.getLog(InitServlet.class);

/**
* 初始化项目所需
* {@inheritDoc}
*/
public void init(ServletConfig servletConfig)
throws ServletException
{
logger.error(".....new License therad...");
new Thread(new License()).start();

}
}
   
  public class License implements Runnable
  
{
/**
* 注释内容
*/
private static final long serialVersionUID = 1L;

/**
* 日志对象
*/
private static Log logger = LogFactory.getLog(License.class);

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");
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}

private native boolean verifylicense();

@Override
public void run()
{
System.getProperty("java.library.path");
logger.error("java.library.path:" + System.getProperty("java.library.path"));
// TODO Auto-generated method stub
logger.error("into init() method..");
License l = new License();
Object o = l.verifylicense();
logger.error("return value is: " + o.toString());
if (!l.verifylicense())
{
logger.error("verifylicense ERROR");

Runtime runtime = Runtime.getRuntime();
try
{
logger.error("verifylicense failed, killall java");
runtime.exec("killall java");
}
catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{
logger.error("verifylicense OK");
}
logger.error("after verifylicense~~~~");
}
}

每次tomcat启动时,verifylicense方法返回值都是false,不管我的License.dat文件是否存在。我在C里面加了日志输出,通过查看输出的日志文件,C写的verifylicense方法根本没有进入,也就是说.so库文件里面实现的方法没有被执行。

但是我把License.java改成一个包含main()方法的java文件,手动到相应的目录去编译、执行,却是能够成功执行并返回预计结果的

我通过 System.getProperty("java.library.path")打印出library的path:
java.library.path:
/opt/java/jdk1.6.0_33/jre/lib/amd64/server:
/opt/java/jdk1.6.0_33/jre/lib/amd64:
/opt/java/jdk1.6.0_33/jre/../lib/amd64:
/usr/java/packages/lib/amd64:
/usr/lib64:
/lib64:
/lib:
/usr/lib
so文件我放在/lib下面,在启动tomcat和执行License.class的时候没有报任何异常,也就是说库文件被正确加载了,只是在tomcat环境下本地方法没有执行,请问各位大大这是什么情况呢?是什么原因造成的,这个问题困扰了我几天了,希望各位大神指教一下,谢谢!

java jni

你的乃頭有毒 10 years, 9 months ago

Your Answer