Refer to current filename and line.

Discussion in 'Java' started by Aaron Fude, Jun 11, 2008.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    I remember this was possible in C with macros. Is it possible in Java
    to refer to the current file name and line number as in

    System.out.println( something.that.returns.current.file.name() + "
    printed this line.");

    Many thanks in advance!

    Aaron
    Aaron Fude, Jun 11, 2008
    #1
    1. Advertising

  2. Aaron Fude

    Mark Space Guest

    Aaron Fude wrote:
    > Hi,
    >
    > I remember this was possible in C with macros. Is it possible in Java
    > to refer to the current file name and line number as in
    >
    > System.out.println( something.that.returns.current.file.name() + "
    > printed this line.");
    >
    > Many thanks in advance!
    >
    > Aaron


    This is all I could find:

    <http://forum.java.sun.com/thread.jspa?threadID=5126327&messageID=9448610>
    Mark Space, Jun 11, 2008
    #2
    1. Advertising

  3. Aaron Fude

    Neil Coffey Guest

    Mark Space wrote:

    >> I remember this was possible in C with macros. Is it possible in Java
    >> to refer to the current file name and line number as in


    > This is all I could find:
    >
    > <http://forum.java.sun.com/thread.jspa?threadID=5126327&messageID=9448610>


    As of Java 5, you can also go via the ThreadMXBean.getThreadInfo().
    I've not tested, but I wonder if there could be a sliiight performance
    gain because you can specify that you only need the top stack trace
    element(s). (The code would be uglier, of course.)

    You can also call Thread.currentThread().getStackTrace(), but internally
    this appears to just create a Throwable and query it, so it depends
    which code you like looking at...!

    Neil
    Neil Coffey, Jun 11, 2008
    #3
  4. Aaron Fude

    Roedy Green Guest

    On Tue, 10 Jun 2008 19:35:55 -0700 (PDT), Aaron Fude
    <> wrote, quoted or indirectly quoted someone who
    said :

    >System.out.println( something.that.returns.current.file.name() + "
    >printed this line.");


    See http://mindprod.com/jgloss/trace.html

    do a new Exception() then use the methods for exam inning the stack
    track fields.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,005
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,437
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,743
    alok000707
    Jul 13, 2010
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    734
    Jonathan N. Little
    May 30, 2008
  5. Stefano Crocco
    Replies:
    0
    Views:
    133
    Stefano Crocco
    Nov 14, 2008
Loading...

Share This Page