Java JNA ―― JNI的加强版

在开发过程中,Java程序猿有时候很郁闷,要取计算机的相关信息相当无措!其实了解JNI的猿人应该知道,通过JNI调用C/C++的动态链接库就可以轻松的实现!但是有人又会觉得JNI还是有点复杂,好吧,今天就分享一下JNI的加强版――JNA,它让我们轻松了跨语言的调用,调用其他语言的方法/函数就如调用Java自己定义方法一般!好啦,不多说,直接贴代码,简单的实例,获取计算机的名称:

/**
 * @Description: 
 *
 * @Title: JNATestServlet.java
 * @Package com.joyce.jna
 * @Copyright: Copyright (c) 2014
 *
 * @author Comsys-LZP
 * @date 2014-2-27 下午04:26:21
 * @version V2.0
 */
package com.joyce.jna;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.jna.Library;
import com.sun.jna.Native;
/**
 * @Description: 
 *
 * @ClassName: JNATestServlet
 * @Copyright: Copyright (c) 2014
 *
 * @author Comsys-LZP
 * @date 2014-2-27 下午04:26:21
 * @version V2.0
 */
public class JNATestServlet extends HttpServlet {
    
    /**
      * @Fields serialVersionUID : TODO
      */
    private static final long serialVersionUID = -8617706236668864317L;
    public interface GetComputerName extends Library {
        GetComputerName INSTANCE = (GetComputerName) Native.loadLibrary("dll/GetCompName",GetComputerName.class);
        public String GetCopmuterName();
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String computerName = GetComputerName.INSTANCE.GetCopmuterName();
        System.out.println(computerName);
        PrintWriter out = response.getWriter();
        out.print(computerName);
        out.flush();
        out.close();
    }
}

这是后台是的代码,至于怎么展示在前台上就不用我多说了吧!

 

动态链接库dll、jar包下载地址:http://download.csdn.net/download/luo201227/7133593

转载至:http://blog.csdn.net/luo201227/article/details/22779341

此条目发表在Java分类目录。将固定链接加入收藏夹。

发表评论