Ayusman
Ayusman

Reputation: 8719

Viewing the calls stack for a particular method

Is it possible to view the complete list of call stack for a given method in my application using any tool?

I want to see the similar result that I can see in JProfiler for a given method.

However I am looking for some open source/free alternative and without having to do too much of configuration setup.

Upvotes: 1

Views: 87

Answers (3)

Alper Akture
Alper Akture

Reputation: 2465

JConsole, which comes with the jdk has a Threads tab, that lists all the running threads. When you select one, it will show the trace. You can connect to a local process without any setup. If you're remote, you have to enable the remote connector as a start up option. Screen shot is from jdk 1.7. JConsole

Upvotes: 1

Pshemo
Pshemo

Reputation: 124215

I am not sure but maybe you are looking something like this method

class MyInfo{
    static void showStack(){
        try{
            throw new Exception();
        }catch(Exception e){
            StackTraceElement[] ste=e.getStackTrace();
            for(int i=1; i<ste.length;i++){
                System.out.println(ste[i].getClassName()+"."+ste[i].getMethodName());
            }
        }
    }
}

Lets try it

class C1 {
    void methodA() {MyInfo.showStack();}
    void methodB() {methodA();}
}

class C2 {
    static void methodX() {
        C1 c1 = new C1();
        c1.methodB();
    };
    //TEST
    public static void main(String[] args) {
        C2.methodX();
    }
}

out

C1.methodA
C1.methodB
C2.methodX
C2.main

Upvotes: 1

Marvo
Marvo

Reputation: 18133

Would this get you what you want?

java.lang.Throwable.getStackTrace()

Upvotes: 0

Related Questions