Catching a main class

Tags

  • java

Tidbit. Spring raises project entry point issue to the JVM. Stack trace is always iterated from topmost element, though. Call multiple main methods in different classes for misery and loss.

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 ignore) { }
  return null;
}