A-A+
Get main class name
1 2 3 4 5 6 7 8 9 | Map a = Thread.getAllStackTraces(); for(Thread t : a.keySet()){ if ("main".equals(t.getName())){ StackTraceElement[] stack = t.getStackTrace (); StackTraceElement main = stack[stack.length - 1]; String mainClass = main.getClassName (); System.out.println(mainClass); } } |
From the commnets, we get a new way~
1 2 3 4 5 | System.out.println( new Throwable(). getStackTrace()[(new Throwable().getStackTrace()).length - 1]. getClassName() ); |
… or in one line
System.out.println(((new Throwable()).getStackTrace()[(new Throwable().getStackTrace()).length – 1]).getClassName());
Thanks, It’s seems simpler.