0

I want to find the line number of a code dynamically. How can this be done?

public static void main(String[] args) {

        System.out.println("This Line Number is : ????");
 }
thelaws
  • 7,991
  • 6
  • 35
  • 54
fdurmus77
  • 516
  • 7
  • 21
  • In general the use of a logger is preferable: `Logger.getAnonymousLogger().log(Level.INFO, "some message");` The format in general contains class name and method, but line number is no problem. – Joop Eggen Oct 21 '14 at 15:10

2 Answers2

2

you can get line number from getStackTrace()[]

 public static void main(String[] arg)
 {

     System.out.println("This Line Number is : "+Thread.currentThread().getStackTrace()[1].getLineNumber());
 }
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

Is it compulsory to use your own log functions? It will be easier to use log4j or any other "standard" log libray

cgimeno
  • 56
  • 6