无法初始化类com.sun.jna.Native



我试图使用JNA更改linux服务器上程序上传文件的文件权限。我的参考资料是这个和这个。下面给出了我的代码。我得到了一个Operation not permitted异常。有什么办法解决这个问题吗?有没有其他方法可以通过程序更改上传文件的权限?或者有没有任何方法可以使用指定的文件权限上传文件。我使用的是java 1.5。我已经把jna.jar放在了/public_html/WEB-INF/lib中,有人能为初学者推荐一个好的jna教程吗?

JSP代码(用于测试)

<%@page import="cc.FileModifierLinux"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try
{

    FileModifierLinux flx=new FileModifierLinux();
    String pathX = getServletContext().getRealPath("/testpage.jsp");
    flx.Update(pathX);
    out.println("No Exception");
}
catch(Exception exp)
{
    out.println("exp :"+exp);
}
%>
</body>
</html>

使用的类别

package cc;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class FileModifierLinux {
     CLibrary libc = (CLibrary) Native.loadLibrary("c", CLibrary.class);
    public void Update(String pth) {
        libc.chmod(pth, 0755);
    }
}
interface CLibrary extends Library {
    public int chmod(String path, int mode);
}

例外

有关完整的异常详细信息,请参阅本

 org.apache.jasper.JasperException: Exception in JSP: /index_check.jsp:23
20: {
21:     
22:     
23:     FileModifierLinux flx=new FileModifierLinux();
24:     String pathX = getServletContext().getRealPath("/testpage.jsp");
25:     flx.Update(pathX);
26:     out.println("No Exception");

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
javax.servlet.ServletException: Could not initialize class com.sun.jna.Native

我也遇到过类似的问题;对我来说,从JNA 3.0.9升级到3.2.7解决了这个问题。我还添加了platform.jar,这是我以前缺少的。

您可能需要在服务器上安装libjnidispatch.so,使其在java.library.path中可用,以便JNA可以加载它。引发的异常是由于JNA找不到该库,并试图从自己的jar文件中解包。

基于最初的异常,servlet将不允许加载本机库,除非是从指定的受保护位置加载。查看servlet容器文档,了解如何安装并使其可用于加载JNI库。

相关内容

最新更新