java.io.IOException: 无法获得锁定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.IOException: Couldn't get lock for
提问by Catheryan
Get error as
获取错误为
"java.io.IOException: Couldn't get lock for ..\log\abc.log", not sure why this happen.
“java.io.IOException:无法锁定 ..\log\abc.log”,不知道为什么会发生这种情况。
Can somebody help me out? Thank you
有人可以帮我吗?谢谢
logger = LogManager.getLogManager().getLogger(className);
FileHandler logFile = new FileHandler(file);
// create txt Formatter
SimpleFormatter formatterTxt = new SimpleFormatter();
logFile.setFormatter(formatterTxt);
logger.addHandler(logFile);
回答by Mukus
I think this is due to there being more than one instance of the logger that has it open or some other external application has it open with a read/write lock.
我认为这是因为有不止一个记录器实例打开它,或者其他一些外部应用程序用读/写锁打开它。
回答by Jeril Kuruvila
@Catheryan
@凯瑟琳
I also had the same issue. For me issue was:
我也有同样的问题。对我来说问题是:
private static final String FILE_PATH="/home/jeril/Logs.log";
handler = new FileHandler(FILE_PATH);
FILE_PATH was WRONG
FILE_PATH 错误
回答by Curious
This issue is a long standing bug in FileHandler
(reported way back in March 2005, in Java 4):
这个问题是一个长期存在的错误FileHandler
(早在 2005 年 3 月,在 Java 4 中报告):
JDK-6244047 : impossible to specify directories to logging FileHandler unless they exist
JDK-6244047:除非存在,否则无法指定目录以记录 FileHandler
Quoting from the description of this bug:
引用此错误的描述:
If you specify a patern to the FileHandler that contains directories that don't currently exist then the FileHandler will fail to initialise even if the directories can be created
如果您指定的 FileHandler 包含当前不存在的目录的模式,则即使可以创建目录,FileHandler 也将无法初始化
Its mentioned that it has been fixed in Java 8. For versions earlier than 8, the workaround is to either not use any directory in the pattern used for a FileHandler
or use a directory which actually exists on the disk.
它提到它已在 Java 8 中修复。对于 8 之前的版本,解决方法是不使用用于 a 的模式中的任何目录,FileHandler
或者使用磁盘上实际存在的目录。