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,430 points
180 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 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,880 points
50 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
286 views
+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,380 points

edited Nov 6, 2018 by Omkar 11,473 views
0 votes
2 answers

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

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 41,000 points
220 views
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,300 points
84 views
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,350 points
139 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,350 points
43 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,050 points
98 views
0 votes
1 answer

How to remove ‘char’ from a String?

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

answered May 21, 2018 in Java by code.reaper12
• 3,450 points
44 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,450 points
221 views