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 17, 2018 in Java by misc.edu04
• 1,450 points
1,589 views

1 answer to this question.

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) {
            System.out.println(element);
        }
    }
 
    public static void main(String[] args) {
        dumpCurrentStackTrace();
    }
}​

This will give you the below output:

java.lang.Thread.getStackTrace(Unknown Source)

DemoDumpThread.dumpCurrentStackTrace(DemoDumpThread.java:3)

DemotDumpThread.main(DemotDumpThread.java:10)

answered May 17, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
1 answer

How can I get the current stack trace in Java?

Hello @kartik, Try this: Thread.currentThread().getStackTrace(); is fine if you don't ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
661 views
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 31, 2018 in Java by Sushmita
• 6,910 points
709 views
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
2,428 views
0 votes
1 answer

How to determine programmatically the current active profile using Spring boot ?

Hello, It doesn't matter is your app Boot ...READ MORE

answered May 21, 2020 in Java by Niroj
• 82,880 points

edited Mar 4, 2022 by Sarfaraz 7,288 views
0 votes
1 answer

difference between wait() and sleep() in java

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

answered Apr 26, 2018 in Java by developer_1
• 3,320 points
853 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

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

answered May 1, 2018 in Java by sharth
• 3,370 points
897 views
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,370 points
695 views
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,120 points
536 views
0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
500 views
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,500 points
2,577 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP