wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to print stack trace of main thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to print stack trace of main thread" Watch "how to print stack trace of main thread" New topic
Author

how to print stack trace of main thread

Rajiv Rai
Ranch Hand

Joined: Jun 14, 2010
Posts: 57


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

Joined: Mar 26, 2012
Posts: 186

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.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
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

Joined: Jun 14, 2010
Posts: 57
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

Joined: Aug 07, 2007
Posts: 2173
    
  47
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print stack trace of main thread
 
Similar Threads
method chaining
Extends
Exception Handling
Need Help on More Anonymous Classes
Problem -- Virtual Method Invocation