如何将文件编写器的路径设置为使用它的 jar 的位置?
How to set the path of the file writer to location of the jar using it?
所以我最近从服务启动 jar 文件,而不是首先转到相关目录的脚本。有没有办法确保使用 jar 文件的路径而不是服务启动的路径?在应用程序内部,我使用下面的代码来获取正确的路径。
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if( workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
将您的工作路径存储为系统 属性:
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if(workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
System.setProperty("tinylog.directory", workPath); // Set work path as system property
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
在tinylog.properties中使用系统属性:
writer = file
writer.file = #{tinylog.directory}/log.txt
writer.format = {date: HH:mm:ss.SSS} {level}: {message}
所以我最近从服务启动 jar 文件,而不是首先转到相关目录的脚本。有没有办法确保使用 jar 文件的路径而不是服务启动的路径?在应用程序内部,我使用下面的代码来获取正确的路径。
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if( workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
将您的工作路径存储为系统 属性:
try {
Path p = Path.of(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
workPath = p.getParent().toString();
if(workPath.matches(".*[lib]")) { // Meaning used as a lib
workPath = Path.of(workPath).getParent().toString();
}
System.setProperty("tinylog.directory", workPath); // Set work path as system property
settingsFile = Path.of(workPath, "settings.xml");
} catch (URISyntaxException e) {
Logger.error(e);
}
在tinylog.properties中使用系统属性:
writer = file
writer.file = #{tinylog.directory}/log.txt
writer.format = {date: HH:mm:ss.SSS} {level}: {message}