问题
// UPDATE 1:
I did some more testing, removed most of the libs and commented out lib specific code resulting in the same behaviour which leeds me to the conclusion, that this problem is not (directly) caused by those libs but seems to be a general issue in my code and/or setup.
The main problem here is that I do not understand why it runs perfectly when launched from within eclipse (instant startup, high performance, etc) while the same code has the described problems as soon as i launch it outside of eclipse (as runnable JAR using the same JDK!).
Can someone shed some light onto what differences there might be?
// END OF UPDATE 1
// Original Post:
comming from an older question asked by me here: Wrapped .exe with launch4j and jdk8 takes very long to start. Using jdk7 instead starts almost instant
Now i know that it is NOT related to launch4j with jdk8 but it seems to be caused by log4j in combination of jdk8 in my application. Similar problems were discusses here: Log4j 2 hangs when creating logger and here: log4j LogManager.getLogger get's stuck in an infinite loop.
But none of the solutions work for me. The problem is a little bit different for me, too. Here we go:
The main Method of my application does some init stuff prior to initializing the Logger object (like cleaning up older log files, etc.). Each step is printed out for debugging purposes via System.out.println. Here is a reduced example:
public class MyTestCase {
private static Logger logger;
public static void main(String[] args) throws Exception {
System.out.println("Executing MyTestCase...");
doInitstuff1();
doInitstuff2();
System.out.println("Initializing Logger...");
logger = LogManager.getLogger(MyTestCase.class.getName());
System.out.println("Init complete!");
doTheRealStuff();
}
private void doInitstuff1() {
System.out.println("Init Stuff 1...");
}
private void doInitstuff2() {
System.out.println("Init Stuff 2...");
}
private void doTheRealStuff() {
System.out.println("Launching GUI...");
}
}
When i execute the code from within Eclipse (debug or run configuration doesn't matter) the immedeate output (< 1 second to start and finish) is as expected:
Executing MyTestCase...
Init Stuff 1...
Init Stuff 2...
Initializing Logger...
Init complete!
Launching GUI...
When I create a runable JAR or a wrapped .exe (with l4j) and execute the application with a JDK or JRE 8 installed on the target platform the result is this:
Executing MyTestCase...
Init Stuff 1...
Init Stuff 2...
Initializing Logger...
Application hangs/stalls for ~10-45 seconds depending on the device <-- This is the actual problem!
Init complete!
Launching GUI...
When I do exactly the same thing compiled against JDK 7 the "deployed" executable launches as fast as from within Eclipse. This is really strange and i have no clue what to do against it.
My buildpath / libraries used for testing purposes & experiments:
CJWizards-0.22
commons-codec-1.10
commons-io-2.4
commons-lang3-3.4
commons-logging-1.2
guava-20.0
itext-2.1.7
JDatePicker-1.3.5
jna-4.3.0
jna-platform-4.3.0
log4j-api-2.8.1
log4j-core-2.8.1
sl4j-api-1.7.22
slf4j-nop-1.7.22
swingx-all-1.6.5-1
tablelayout
vlcj-3.10.1
zip4j_1.3.2
Any idea what is causing this?
Recap:
running from within eclipse this problem does not occure (no matter if jdk 7 or 8 is used)
running with JDK or JRE 7 as runnable jar or exe the problem does not occure
running with JDK or JRE 8 as runnable jar or exe the problem occurs
Here is the content of my log4j2.xml configuration file, just in case you need it, too:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error">
<appenders>
<File name="MyTestCase.debug" fileName="${sys:user.home}/.mtc/log/MyTestCase.debug.log">
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
<PatternLayout pattern="%d{yyyy-MM-dd | HH:mm:ss.SSS} | [%t] %-5level | %logger{42} | %msg%n"/>
</File>
<Async name="Async.debug">
<appender-ref ref="MyTestCase.debug"/>
</Async>
<File name="MyTestCase.error" fileName="${sys:user.home}/.mtc/log/MyTestCase.error.log">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy-MM-dd | HH:mm:ss.SSS} | [%t] %-5level | %logger{42} | %msg%n"/>
</File>
<Async name="Async.error">
<appender-ref ref="MyTestCase.error"/>
</Async>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Async.debug" level="debug"/>
<appender-ref ref="Async.error" level="error"/>
</root>
</loggers>
</configuration>
回答1:
It's been a while but the problem is "solved" since the project was migrated to JDK 9. I still don't know what was/is causing those issues with JDK 8 but they have disappeared using JDK 9. So i consider this "resolved".
来源:https://stackoverflow.com/questions/42830383/logmanager-getlogger-stalls-application-for-10-30-seconds-when-using-jdk8