Skip to content

Commit

Permalink
fix(core): 修复CoreUtil.getRootPath()问题 (#686)
Browse files Browse the repository at this point in the history
在jar运行环境下,根路径获取错误
  • Loading branch information
unknowIfGuestInDream committed Sep 1, 2023
1 parent 4f94d2d commit 24e5496
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions core/src/main/java/com/tlcsdm/core/util/CoreUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import cn.hutool.log.StaticLog;
import com.tlcsdm.core.annotation.Order;

import java.awt.*;
import java.awt.Desktop;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand All @@ -42,6 +42,7 @@
import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -163,14 +164,25 @@ public static void sortByOrder(List<Class<?>> list) {
*/
public static String getRootPath() {
String path = System.getProperty("user.dir");
if (isStartupFromJar(CoreUtil.class)) {
return path;
}
File file = new File(path);
List<File> list = FileUtil.loopFiles(file.toPath(), 1, new FileFilter() {
@Override
public boolean accept(File pathname) {
return "core".equals(pathname.getName());
}
});
return list.size() > 0 ? path : file.getParent();
return !list.isEmpty() ? path : file.getParent();
}

/**
* 判断类是否在jar中
*/
public static boolean isStartupFromJar(Class<?> clazz) {
URL url = clazz.getResource("");
return "jar".equals(url.getProtocol());
}

}

0 comments on commit 24e5496

Please sign in to comment.