java中获取jar路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chushoutaizhong/article/details/83583310
package test.data.com.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * @param
 * @param
 * @param
 * @author HUAZAI
 * @title:
 * @description:
 * @return 返回类型 :
 * <ul>
 * <li></li>
 * <li></li>
 * <ul>
 * @throws
 * @date 18-10-31 上午10:01
 */
public class UtilsPath {
    /**
     * @title
     * @description 取得当前类所在的文件 包括文件
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 11:09:00
     */
    public static Path getClassFilesPath(Class clazz) {
        URL path = clazz.getResource(clazz.getName().substring(
                clazz.getName().lastIndexOf(".") + 1)
                + ".class");
        if (path == null) {
            String name = clazz.getName().replaceAll("[.]", "/");
            path = clazz.getResource("/" + name + ".class");
        }
        return Paths.get(path.getPath()).toAbsolutePath();
    }

    /**
     * @title
     * @description 同getClassFilesPath 解决中文编码问题
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 11:08:37
     */

    public static String getClassFilesPathStr(Class clazz) {
        try {
            return java.net.URLDecoder.decode(getClassFilesPath(clazz).toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * @title
     * @description 取得当前类所在的ClassPath目录
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 11:10:13
     */

    public static Path getClassDirPath(Class clazz) {
        Path path = getClassFilesPath(clazz);
        for (int index = path.getNameCount(); index >= 0 && StringUtils.lastIndexOf(StringUtils.lowerCase(path.toString()), ".jar!") > 0; index--) {
            path = path.subpath(0, index);
        }
        return path;
    }

    /**
     * @title
     * @description 同getClassDirPath 解决中文编码问题
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 11:13:11
     */

    public static String getClassDirPathStr(Class clazz) {
        try {
            return java.net.URLDecoder.decode(getClassDirPath(clazz).toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * @title
     * @description 获取当前jar包所在的目录
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 10:35:42
     */

    public static Path getJarFilePath(Class clazz) {
        Path path = Paths.get(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
       return path.toAbsolutePath();
    }

    /**
     * @title
     * @description 获取jar文件路径 解决中文乱码
     * @author HUAZAI
     * @param 
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>    
     * @return 
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul> 
     * @throws 
     * @date 2018-10-31 10:37:17
     */
    
    public static String getJarPath(Class clazz) {
        try {
            return java.net.URLDecoder.decode(getJarFilePath(clazz).toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
    /**
     * @title
     * @description 获取jar文件所在文件夹路径 解决中文乱码
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 10:37:17
     */

    public static String getJarDirPath(Class clazz) {
        try {
            return java.net.URLDecoder.decode(getJarFilePath(clazz).getParent().toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * @title
     * @description 加载配置文件 配置文件所在文件夹位置是Class的jar包所在的位置
     * @author HUAZAI
     * @param
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @return
     *      <ul>
     *          <li></li>
     *          <li></li>
     *      <ul>
     * @throws
     * @date 2018-10-31 14:10:14
     */

    public static Properties reloadConfig(Class clazz) {
        Properties pro = new Properties();
        String jarDirPath = getJarDirPath(clazz);
        Path path = Paths.get(jarDirPath, "args.properties");
        try (FileInputStream in = new FileInputStream(path.toString())){
            pro.load(in);
            return pro;
        } catch (IOException e) {
            return pro;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(getClassFilesPathStr(UtilsPath.class));
        System.out.println(getClassDirPathStr(UtilsPath.class));
        System.out.println(getJarPath(UtilsPath.class));
        System.out.println(getJarDirPath(UtilsPath.class));
        System.out.println(System.getProperty("user.dir"));//这种方式不靠谱 这是在哪里执行命令 获取的路径就是哪里
    }
}

猜你喜欢

转载自blog.csdn.net/chushoutaizhong/article/details/83583310