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 251 views
1 answer 246 views
1 answer 235 views
1 answer 198 views
2 answers 247 views
247 views asked Mar 18, 2023 by avibootz
1 answer 214 views
2 answers 318 views
...