zoukankan      html  css  js  c++  java
  • 获取jar绝对路径

    package Io;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    public class JarToolUtil {
    /**
    * 获取jar绝对路径
    *
    * @return
    */
    public static String getJarPath()
    {
    File file = getFile();
    if (file == null)
    return null;
    return file.getAbsolutePath();
    }

    /**
    * 获取jar目录
    *
    * @return
    */
    public static String getJarDir()
    {
    File file = getFile();
    if (file == null)
    return null;
    return getFile().getParent();
    }

    /**
    * 获取jar包名
    *
    * @return
    */
    public static String getJarName()
    {
    File file = getFile();
    if (file == null)
    return null;
    return getFile().getName();
    }

    /**
    * 获取当前Jar文件
    *
    * @return
    */
    private static File getFile()
    {
    // 关键是这行...
    String path = JarToolUtil.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    try
    {
    path = java.net.URLDecoder.decode(path, "UTF-8"); // 转换处理中文及空格
    }
    catch (java.io.UnsupportedEncodingException e)
    {
    return null;
    }
    return new File(path);
    }

    /**
    * 获取当前目录下的文件
    */
    public static List<String> getFile(String path) {
    File file = new File(path);

    File[] fileList = file.listFiles();
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].isFile()) {
    String fileName = fileList[i].getName();
    list.add(fileName);
    }
    /*if (fileList[i].isDirectory()) {
    String fileName = fileList[i].getName();
    System.out.println("目录:" + fileName);
    }*/
    }
    return list;
    }
    }

  • 相关阅读:
    常用 SQL Server 规范集锦
    让Git忽略所有obj和bin目录的同步
    Sql server 存储过程基础语法
    nginx 站点代理,负载均衡
    CentOS7.0安装Nginx-1.12.0
    CentOS7安装GNOME可视化界面和如何配置IP地址
    开发工具资料集合
    NOIP2018总结反思
    NOIP2018考试报告
    STL基础用法
  • 原文地址:https://www.cnblogs.com/developer-ios/p/12438635.html
Copyright © 2011-2022 走看看