Print current code line number

Do you want to print the line number of the code currently in execution in a Java program. You can use this:

System.out.println("The line number is " + new Exception().getStackTrace()[0].getLineNumber());

Comments

Jo Khuan said…
ty very much for ur sincere help
Anonymous said…
if you define a function for debugging output you need to make this ...getStackTrace()[1]... to get the line number of the debugging calling code
Anonymous said…
Thanks. Just what I needed.
Cheers.
andrew said…
ty very much!I found it for a long time!