How to retrieve current stack trace?

0 votes

Do we have any method in Java which can fetch the current stack trace apart from Thread.dumpStack()? 

But, I don’t want to print the stack trace, I just need to fetch it.

To be more specific, what I want is a method which works similar to Environment.StackTrace in .NET.

May 16, 2018 in Java by misc.edu04
• 1,380 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Yes, in Java we do have a similar function called Thread.currentThread().getStackTrace();

This method will return you the array of StackTraceElement. The array returned gives the current stack trace. Below is a demo to implement this method:

public class TestDumpThread {
        //Dump the current thread stack trace.
    public static void dumpCurrentStackTrace() {
        StackTraceElement[] stes = Thread.currentThread().getStackTrace();
        for (StackTraceElement element : stes) {
    public static void main(String[] args) {

This will give you the below output:

java.lang.Thread.getStackTrace(Unknown Source)



answered May 16, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
2 answers

How to find out current working directory in Java?

You can also use java.nio.file.Path and java.nio.file.Paths. Path ...READ MORE

answered Jul 30, 2018 in Java by Sushmita
• 6,840 points
0 votes
1 answer

How to retrieve column names from java.sql.ResultSet?

You may refer the below code: ResultSet ...READ MORE

answered Jul 4, 2018 in Java by sophia
• 1,400 points
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,360 points

edited Nov 6, 2018 by Omkar 5,203 views
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,270 points
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

How to convert s Stack trace to a String?

One can use the given method to ...READ MORE

answered Jun 25, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

What are Daemon threads in Java?

A daemon thread is a thread that is doing ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,040 points
0 votes
1 answer

How to remove ‘char’ from a String? you can try using the overloaded ...READ MORE

answered May 21, 2018 in Java by code.reaper12
• 3,450 points
0 votes
1 answer

How to download a file from spring controllers?

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void ...READ MORE

answered Sep 4, 2018 in Java by code.reaper12
• 3,450 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.