How to get the current source file name and line number in Java

1 Answer

0 votes
public class JavaApplication {
    public static void Log() {
        StackTraceElement stack = Thread.currentThread().getStackTrace()[2]; 

        System.out.println("File: " + stack.getFileName());
        System.out.println("Line: " + stack.getLineNumber());
    }
    
    public static void main(String[] args) {
        Log();
    }
}




/*
run:

File: JavaApplication.java
Line: 10
    
*/
 

 



answered Jun 11, 2024 by avibootz

Related questions

1 answer 129 views
1 answer 137 views
1 answer 116 views
1 answer 123 views
2 answers 200 views
1 answer 148 views
...