简体   繁体   English

FileHandler 创建给出 NoSuchFileException 导致 missing.lck

[英]FileHandler creation gives NoSuchFileException cause missing .lck

im searching for 2 hours now fo a solution to this:我现在搜索了 2 个小时来解决这个问题:

Im trying to add a FileHandler to a Logger in a JAR.我试图在 JAR 中将FileHandler添加到Logger The Log-Directory is outside.日志目录在外面。

In my IDE (NetBeans) this works:在我的 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());
    }

But in my Jar i keep getting this for every Logger:但在我的 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)

Of course the.lck doesnt exist, the Handler gets created right now...当然 .lck 不存在,处理程序现在被创建......

Same error if i use p.normalize().toAbsolutPath() .如果我使用p.normalize().toAbsolutPath()出现同样的错误。 I cant use a Stream, cause its the creation of a FileHandler... Like mentioned in any Solution i found...我不能使用 Stream,因为它创建了 FileHandler ......就像我发现的任何解决方案中提到的......

Why does this happen?为什么会这样? How to prevent this?如何防止这种情况?

Well... Thanks VGR...嗯...谢谢VGR...

i just added this right before the FileHandler Initialisation.. now it works wevery time.. I maybe was just stupid, thinking the directory was created before from the FileHandler not the IDE.我刚刚在 FileHandler 初始化之前添加了这个。现在它每次都可以工作。我可能只是愚蠢,认为该目录是从 FileHandler 而不是 IDE 之前创建的。

if (!Files.exists(p.getParent())) {
            Files.createDirectory(p.getParent());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM