Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to get the current stack trace in Java

3 Answers

0 votes
import java.util.Arrays;
 
public class MyClass {
    public static void f2() {
        
        System.out.println("f2()");
    }
    
    public static void f1() {
        
        System.out.println("f1()");
        f2();
    }
    
    public static void main(String args[]) {
        
        f1();

        Thread currentThread = Thread.currentThread();
        
        StackTraceElement[] stackTrace = currentThread.getStackTrace();
        
        System.out.println(Arrays.toString(stackTrace));
    }
}
  
  
  
  
/*
run:
  
f1()
f2()
[java.base/java.lang.Thread.getStackTrace(Thread.java:1610), MyClass.main(MyClass.java:21)]
  
*/

 



answered Oct 27, 2023 by avibootz
0 votes
public class MyClass {
    public static void f2() {
        
        System.out.println("f2()");
    }
    
    public static void f1() {
        
        System.out.println("f1()");
        f2();
    }
    
    public static void main(String args[]) {
        
        f1();

        Thread currentThread = Thread.currentThread();
        
        StackTraceElement[] stackTrace = currentThread.getStackTrace();
        
        for (StackTraceElement element : stackTrace) {
            System.out.println(element);
        }
    }
}
  
  
  
  
/*
run:
  
f1()
f2()
java.base/java.lang.Thread.getStackTrace(Thread.java:1610)
MyClass.main(MyClass.java:21)
  
*/

 



answered Oct 27, 2023 by avibootz
0 votes
public class MyClass {
    public static void f2() {
        
        System.out.println("f2()");
    }
    
    public static void f1() {
        
        System.out.println("f1()");
        f2();
    }
    
    public static void main(String args[]) {
        
        f1();

        Thread currentThread = Thread.currentThread();
        
        Throwable t = new Throwable();
        StackTraceElement[] stackTrace = t.getStackTrace();
        
        for (StackTraceElement element : stackTrace) {
            System.out.println(element);
        }
    }
}
  
  
  
  
/*
run:
  
f1()
f2()
MyClass.main(MyClass.java:19)
  
*/

 



answered Oct 27, 2023 by avibootz

Related questions

...