Skip to content

Commit

Permalink
fix bug: jar包运行时错误
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashuaizhang committed Dec 13, 2021
1 parent dd9d93f commit 7af369c
Showing 1 changed file with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.sql.DataSource;
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardCopyOption;

Expand Down Expand Up @@ -44,37 +45,43 @@ public DataSource dataSource(DataSourceProperties properties) {
}

private void extractDb7zFile() {
URL dbResource = getClass().getResource("/static/yyets.db");
if (dbResource != null) {
log.info("检测到数据源文件: {}", URLDecoder.decode(dbResource.getPath(), StandardCharsets.UTF_8));
return;
}
final String moduleName = "/yyets-history";
String projectPath = System.getProperty("user.dir");
projectPath = projectPath.replaceAll("\\\\", "/");
if(!projectPath.endsWith(moduleName)) {
if (!projectPath.endsWith(moduleName)) {
projectPath = projectPath + moduleName;
}
String staticResourcePath = projectPath + "/src/main/resources/static/";
String dbFilePath = staticResourcePath + "yyets.db";
File dbFile = new File(dbFilePath);
if(!dbFile.exists()) {
String db7zFilePath = staticResourcePath + "yyets.7z";
File db7zFile = new File(db7zFilePath);
if(!db7zFile.exists()) {
throw new IORuntimeException("数据源文件缺失并且无法找到压缩包: " + db7zFilePath);
}
log.info("开始解压数据源文件: {}", db7zFilePath);
try (SevenZExtractor sevenZExtractor = new SevenZExtractor(db7zFile)) {
sevenZExtractor.extract(new File(staticResourcePath));
if(dbFile.exists()) {
log.info("数据源文件自动解压完成: {}", dbFilePath);
String classPath = getClass().getResource("/static").getPath();
classPath = URLDecoder.decode(classPath, StandardCharsets.UTF_8);
FileUtil.copyFile(dbFile, new File(classPath), StandardCopyOption.REPLACE_EXISTING);
log.info("复制数据源文件至classpath: {}", classPath);
} else {
throw new IORuntimeException("未知的解压异常: " + db7zFilePath);
}
} catch (Exception e) {
String msg = "数据源文件自动解压失败,请手动解压文件: resources/static/yyets.7z";
throw new IORuntimeException(msg, e);
if (dbFile.exists()) {
throw new IllegalStateException("请重新编译项目");
}
String db7zFilePath = staticResourcePath + "yyets.7z";
File db7zFile = new File(db7zFilePath);
if (!db7zFile.exists()) {
throw new IORuntimeException("数据源文件缺失并且无法找到压缩包: " + db7zFilePath);
}
log.info("开始解压数据源文件: {}", db7zFilePath);
try (SevenZExtractor sevenZExtractor = new SevenZExtractor(db7zFile)) {
sevenZExtractor.extract(new File(staticResourcePath));
if (dbFile.exists()) {
log.info("数据源文件自动解压完成: {}", dbFilePath);
String classPath = getClass().getResource("/static").getPath();
classPath = URLDecoder.decode(classPath, StandardCharsets.UTF_8);
FileUtil.copyFile(dbFile, new File(classPath), StandardCopyOption.REPLACE_EXISTING);
log.info("复制数据源文件至classpath: {}", classPath);
} else {
throw new IORuntimeException("未知的解压异常: " + db7zFilePath);
}
} catch (Exception e) {
String msg = "数据源文件自动解压失败,请手动解压文件: resources/static/yyets.7z";
throw new IORuntimeException(msg, e);
}
}
}

0 comments on commit 7af369c

Please sign in to comment.