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 137 views
1 answer 117 views
1 answer 128 views
1 answer 104 views
2 answers 136 views
136 views asked Mar 18, 2023 by avibootz
1 answer 122 views
2 answers 183 views
...