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 140 views
1 answer 147 views
1 answer 119 views
1 answer 129 views
2 answers 206 views
1 answer 157 views
...