How to get the name of the calling class in Java?

Easiest way is the following: String className = new Exception().getStackTrace()[1].getClassName(); But in real there should be no need for this, unless for some logging purposes, because this is a fairly expensive task. What is it, the problem for which you think that this is the solution? We may come up with -much- better suggestions. Edit: … Read more

What causes a java.lang.StackOverflowError

Check for any recusive calls for methods. Mainly it is caused when there is recursive call for a method. A simple example is public static void main(String… args) { Main main = new Main(); main.testMethod(1); } public void testMethod(int i) { testMethod(i); System.out.println(i); } Here the System.out.println(i); will be repeatedly pushed to stack when the … Read more

print call stack in C or C++

For a linux-only solution you can use backtrace(3) that simply returns an array of void * (in fact each of these point to the return address from the corresponding stack frame). To translate these to something of use, there’s backtrace_symbols(3). Pay attention to the notes section in backtrace(3): The symbol names may be unavailable without … Read more