> I am trying to add some debug print statements to my java code. I
> would like the print statement to display the file name and line number
[quoted text clipped - 4 lines]
>
> Does anyone know of a way to do this?
>> I am trying to add some debug print statements to my java code. I
>> would like the print statement to display the file name and line number
[quoted text clipped - 6 lines]
> Throwable object or from a Thread object (if you are using JDK 1.5).
> From the stack trace elements you can get the information you need.
You can use the static Thread.dumpStack() method, which is available prior
to 1.5. But you still get the whole stack trace rather than just the file
and line number. If you use a logging framework such as Log4J
(http://logging.apache.org) you can output this information without the
stack trace more easily (but this information is expensive to generate and
is not available if you compiled your code with the debug information
omitted).
You don't really need the file name, you can just output the class name
(this.getClass()), which is pretty much equivalent. The line number is
not particularly helpful unless you have multiple debug statements in the
same file that print out the same message.
Dan.

Signature
Daniel Dyer
http://www.footballpredictions.net
Andrea Desole - 10 Jun 2005 16:49 GMT
> You can use the static Thread.dumpStack() method, which is available
> prior to 1.5. But you still get the whole stack trace rather than just
but dumpStack prints the stack trace, which makes it a bit more
difficult to parse. I still find StackTraceElement[] easier.
Just for completeness, I would also like to show something I just read
on the documentation of Throwable.getStackTrace() (but apparently
generally valid, since it refers to printStackTrace):
Some virtual machines may, under some circumstances, omit one or more
stack frames from the stack trace. In the extreme case, a virtual
machine that has no stack trace information concerning this throwable is
permitted to return a zero-length array from this method. Generally
speaking, the array returned by this method will contain one element for
every frame that would be printed by printStackTrace
> the file and line number. If you use a logging framework such as
> Log4J (http://logging.apache.org) you can output this information
> without the stack trace more easily (but this information is expensive
log4j or any good logging framework is also an option