to print line number in the Java code

How do we get or print line number of the code in Java when we execute the program ? in C we have line , similar way I need in Java
[152 byte] By [mamatha_mallesh] at [2007-9-27 14:35:40]
# 1

The only way I'm aware of ispublic static int lineNumber() {

int line = 0;

try {

throw new RuntimeException();

} catch (RuntimeException re) {

lineNumber = re.getStackTrace()[1].getLineNumber();

}

return line;

}

And you need 1.4 for this to work...

jsalonen at 2007-7-5 22:35:06 > top of Java-index,Java Essentials,Java Programming...
# 2
New answer to an old question: The following works on J2SE 5.0:StackTraceElement[] stes = Thread.currentThread().getStackTrace(); StackTraceElement ste = stes[2]; System.out.println(ste.getLineNumber()); Br, Panu
napu at 2007-7-5 22:35:06 > top of Java-index,Java Essentials,Java Programming...
# 3

It's probably easier to write your own macro expansion preprocessor.

I think you could do it with a one-liner in perl, something like:

perl -pe 's/#LINE#/$./;' MyProg.jv > MyProg.java

I don't suppose the new annotation feature helps out at all? It seems strange that something this commonly needed is so awkward to do.

paulcw at 2007-7-5 22:35:06 > top of Java-index,Java Essentials,Java Programming...