【8.3.0】【rule】更新一个获取jar包当前所在目录的方法

master
stylefeng 2024-08-29 23:13:16 +08:00
parent fcdc64b67e
commit 6089b7c018
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package cn.stylefeng.roses.kernel.rule.util;
import cn.hutool.core.util.ClassUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
/**
* Jar
*
* @author fengshuonan
* @since 2024/8/29 22:54
*/
@Slf4j
public class JarPathUtil {
/**
* jar
*
* @author fengshuonan
* @since 2024/8/29 22:54
*/
public static String getJarPath() {
// 获取当前类的URL
String jarPath = ClassUtil.getClassPath();
// 如果以nested:/开头
if (jarPath.contains("nested:")) {
jarPath = jarPath.substring(7);
}
// 如果路径以"file:/"开头,去掉前缀
if (jarPath.startsWith("file:")) {
jarPath = jarPath.substring(5);
}
// 如果路径以"!"结尾,去掉后缀
if (jarPath.contains("!")) {
jarPath = jarPath.substring(0, jarPath.indexOf("!"));
}
// 获取文件对象
File jarFile = new File(jarPath);
// jar文件所在的目录
File parentDirectory = jarFile.getParentFile();
// 返回绝对路径
return parentDirectory.getAbsolutePath();
}
}