Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to print stack trace of main thread

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Guys , please suggest me how can I get the stack trace of the above method calls
in method3(),Just doing it as experimentation
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably, I did not get what exactly you sought, but for getting stack trace of main thread when an exception occured, you can use following code sample:

how can I get the stack trace of the above method calls in method3()

By printing statements, as you did, in each method, you can trace the flow of method calls. Stack Trace is like information about methods and statements when some exception occurs, and helps you find out the root cause as you navigate to it looking at the information.

Edit: You might check http://www.homeandlearn.co.uk/java/java_stack_trace.html.
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to dump the current stack trace to the console you can use the Thread class' dumpStack() method which outputs the stack trace to stderr. If you want your code to have access to the stack trace, the Thread class has methods for this too.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Threads chapter in the SCJP 5 book


"The main() method, that starts the whole ball rolling, runs in one thread, called
(surprisingly) the main thread. If you looked at the main call stack (and you can, any
time you get a stack trace from something that happens after main begins, but not
within another thread), you'd see that main() is the first method on the stack—
the method at the bottom."


So , all I want is just a stack trace which highlights the order of method calls
in the main thread which as in this code has no exception code at all.

How can this be achieved ?

Thanks
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:How can this be achieved ?

I've already told you, you call the Thread class' dumpStack() method. This will print out the stack trace to the console for the thread that calls the method. ie

If myMethod() is called from the main thread it will print the stack trace for all method calls back to main().

If you want something other than this can you give more details of what you want to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic