How to get the current source file name in Java

1 Answer

0 votes
public class JavaApplication {
    public static String currentFileName() {
        return Thread.currentThread().getStackTrace()[2].getFileName();
    }
    
    public static void main(String[] args) {
        System.out.println(currentFileName());
    }
}
 
 
/*
run:
 
JavaApplication.java
     
*/

 



answered Jun 11, 2024 by avibootz

Related questions

1 answer 137 views
2 answers 183 views
1 answer 117 views
1 answer 128 views
1 answer 135 views
1 answer 110 views
...