How to get the current source line number in Java

1 Answer

0 votes
public class JavaApplication {
    public static int currentLineNumber() {
        return Thread.currentThread().getStackTrace()[2].getLineNumber();
    }

    public static void main(String[] args) {
        System.out.println(currentLineNumber());
    }
}


/*
run:

8
    
*/
 

 



answered Jun 11, 2024 by avibootz
edited Jun 11, 2024 by avibootz

Related questions

1 answer 252 views
1 answer 249 views
1 answer 238 views
1 answer 202 views
2 answers 253 views
253 views asked Mar 18, 2023 by avibootz
1 answer 218 views
2 answers 320 views
...