我现在搜索了 2 个小时来解决这个问题:
我试图将 a 添加FileHandler
到Logger
JAR 中。日志目录在外面。
在我的 IDE (NetBeans) 中,这有效:
public static void addFileHandler(Logger logger) {
try {
Path p = Paths.get("logs", logger.getName().substring(logger.getName().lastIndexOf('.') + 1) + ".log");
FileHandler fh = new FileHandler(p.toString(), false);
fh.setFormatter(sf);
fh.setLevel(Level.ALL);
fh.setEncoding("UTF-8");
logger.addHandler(fh);
if (errorHandler != null) {
logger.addHandler(errorHandler);
}
if (allHandler != null) {
logger.addHandler(allHandler);
}
} catch (IOException | SecurityException ex) {
LOG.log(Level.SEVERE, getString("ERROR WHILE ADDING FILEHANDLER TO LOGGER") + " " + logger.getName(), ex);
return;
}
LOG.log(Level.INFO, getString("LOGGER {0} GOT ADDED HIS FILEHANDLERS SUCCESSFULLY") + "!", logger.getName());
}
但是在我的 Jar 中,我不断为每个 Logger 获取这个:
Juli 10, 2020 2:36:48 PM de.dhbw.mosbach.inf19b.programmieren2.game.utilities.logging.Logging addFileHandler
SEVERE: Fehler beim Hinzufügen vom FileHandler zum Logger de.dhbw.mosbach.inf19b.programmieren2.game.utilities.logging.Formatting
java.nio.file.NoSuchFileException: logs\Formatting.log.lck
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newFileChannel(WindowsFileSystemProvider.java:120)
at java.base/java.nio.channels.FileChannel.open(FileChannel.java:292)
at java.base/java.nio.channels.FileChannel.open(FileChannel.java:345)
at java.logging/java.util.logging.FileHandler.openFiles(FileHandler.java:511)
at java.logging/java.util.logging.FileHandler.<init>(FileHandler.java:341)
at de.dhbw.mosbach.inf19b.programmieren2.game.utilities.logging.Logging.addFileHandler(Logging.java:58)
at de.dhbw.mosbach.inf19b.programmieren2.game.utilities.logging.Formatting.<clinit>(Formatting.java:39)
at de.dhbw.mosbach.inf19b.programmieren2.game.utilities.Constants.<clinit>(Constants.java:34)
at de.dhbw.mosbach.inf19b.programmieren2.game.utilities.logging.Logging.<clinit>(Logging.java:40)
at de.dhbw.mosbach.inf19b.programmieren2.game.main.StartJar.<clinit>(StartJar.java:21)
当然 .lck 不存在,处理程序现在被创建......
如果我使用相同的错误p.normalize().toAbsolutPath()
。我不能使用 Stream,因为它创建了 FileHandler ......就像我发现的任何解决方案中提到的......
为什么会这样?如何防止这种情况?