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 245 views
1 answer 234 views
1 answer 195 views
1 answer 206 views
2 answers 317 views
1 answer 239 views
...