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

标签 java linux jsp file-permissions jna

我试图使用 JNA 更改 Linux 服务器上以编程方式上传的文件的文件权限。我的引用文献是thisthis 。我的代码如下。我收到了不允许操作异常。有什么办法可以解决这个问题吗?还有其他方法可以以编程方式更改上传文件的权限吗?或者有什么办法可以以指定的文件权限上传文件。我正在使用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);
}

异常

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

 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,这是我之前丢失的。

关于java - 无法初始化类 com.sun.jna.Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11789493/

相关文章:

linux - 从 PowerShell 远程执行 Linux 应用程序

java - ubuntu 上的 izpack 安装程序 'is not marked as executable'

javascript - JSPX 将 <?xml version ="1.0"encoding ="UTF-8"?> 添加到页面顶部,导致 IE6 中出现 JS 错误

spring - 在 Spring MVC 和 Hibernate 应用程序的 JSP 页面中实现分页

java - 如何在 CompletableFuture 完成后通过垃圾收集进行回收?

java - TextArea 仅显示一次结果

Java:通过在初始化期间返回现有类来防止重复类

java - 请提示Android WiFi Direct是否支持Multicast Socket通信

linux - 从已安装的 ISO 镜像中提取文件

javascript - 刷新位于不同域的 iframe