简体   繁体   中英

FileHandler creation gives NoSuchFileException cause missing .lck

im searching for 2 hours now fo a solution to this:

Im trying to add a FileHandler to a Logger in a JAR. The Log-Directory is outside.

In my IDE (NetBeans) this works:

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:

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...

Same error if i use p.normalize().toAbsolutPath() . I cant use a Stream, cause its the creation of a FileHandler... Like mentioned in any Solution i found...

Why does this happen? How to prevent this?

Well... Thanks 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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