How can I get the current stack trace in Java

0 votes
How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace?

I found Thread.dumpStack() but it is not what I want - I want to get the stack trace back, not print it out.
Jul 28, 2020 in Java by kartik
• 37,530 points
180 views

1 answer to this question.

0 votes

Hello @kartik,

Try this:

Thread.currentThread().getStackTrace();

is fine if you don't care what the first element of the stack is.

new Throwable().getStackTrace();

will have a defined position for your current method, if that matters.

Hope it works!!
Thank You!!

answered Jul 28, 2020 by Niroj
• 82,880 points

Related Questions In Java

0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
24,384 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,630 points
4,809 views
0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,090 points
796 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,110 points
2,076 views
0 votes
1 answer

How do I convert a String to an int in Java?

Hello @kartik, We can use the parseInt(String str) method of ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
255 views
0 votes
1 answer

How to get an enum value from a string value in Java?

Hello @kartik, Yes, Blah.valueOf("A") will give you Blah.A. Note that the name ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
772 views
0 votes
1 answer

How to avoid Java code in JSP files?

Hello @kartik, JSTL offers tags for conditionals, loops, sets, ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
131 views
0 votes
1 answer

How to directly initialize a HashMap?

Hello @Kartik, In plain java 8 you also ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
272 views
0 votes
1 answer

How to get the current working directory in Java?

Hello, Using java.nio.file.Path and java.nio.file.Paths, you can do the following to ...READ MORE

answered May 28, 2020 in Java by Niroj
• 82,880 points
1,239 views
0 votes
1 answer

How to get the windows native look in Java GUI programming?

Hello Kartik,  You can try the syntax: UIManager.setLookAndFeel(PLAF); (Pluggable ...READ MORE

answered May 7, 2020 in Java by Niroj
• 82,880 points
253 views