Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

How to get line number in java source code?


Hi

can any java guru tell me how get the method starting line number of currently executing method? For example in class A there may be a method

void aa(int c, int d) // line number , say 512
{

// some code

}

I need to get this 512 during execution of this aa.

Your rating: None

try to use log4j with

try to use log4j with pattern %L pattern
It will log the line number

This is simple

This is simple example

public static int getLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();

}

It will log the line

It will log the line number

try to use log4j with pattern %L pattern

try to use log4j with pattern %L pattern, it will log the line number.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.