Find a class where the main method of running

private Class<?> deduceMainApplicationClass() {
    try {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if ("main".equals(stackTraceElement.getMethodName())) {
                return Class.forName(stackTraceElement.getClassName());
            }
        }
    }
    catch (ClassNotFoundException ex) {
        // Swallow and continue
    }
    return null;
}

This is SpringApplication acquired main class method. Always felt this way a bit "setback", ^ _ ^, Spring also did not expect so used, do not know there is no other better way.

Guess you like

Origin www.cnblogs.com/yejg1212/p/11950523.html