Stack trace can be very helpful in figuring out the source of a problem. When an automatic correction is possible, the need arises to read it programmatically.
Since Java 1.4, the current stack trace can be accessed via the java.lang.Thread and java.lang.Throwable classes. You can add the following line to any method of your code:
Thread.currentThread().dumpStack();
You can also add the following line:
new Throwable().printStackTrace();
It will print the stack trace to the standard output. Alternatively, since Java 8, you can use any of the following lines for the same effect:
Arrays.stream(Thread.currentThread().getStackTrace())
.forEach(System.out::println);
Arrays.stream(new Throwable().getStackTrace())
.forEach(System.out::println);
Or you can extract the fully qualified name of the caller class, using one of these lines:
System...