How to get the line number in the code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles

    How to get the line number in the code?

    Is there a way to obtain the line number in my code? For example:
    1 public class MyClass {
    2 int a = 5;
    3 System.out.prin tln("Integer variable a was set on line number: " + x);
    4 etc.
    Where x is equal to '2'.

    How can I obtain 'x' ?

    Thanks.


  • Kathy Benson

    #2
    Re: How to get the line number in the code?

    You cannot do that in Java. The only way to get line number is by
    creating a Throwable object and then extracting the stack trace. You
    cannot find the line number at compile time.

    Regards,
    KB





    Charles wrote:
    [color=blue]
    >Is there a way to obtain the line number in my code? For example:
    >1 public class MyClass {
    >2 int a = 5;
    >3 System.out.prin tln("Integer variable a was set on line number: " + x);
    >4 etc.
    >Where x is equal to '2'.
    >
    >How can I obtain 'x' ?
    >
    >Thanks.
    >
    >
    >
    >[/color]

    --
    ------------------------------------------------------------------------
    This email is certified to be Spam free by Spam Marshall

    You don't pay to get spam, why pay to clean it?
    Visit http://www.spammarshall.com to create an account for free
    <http://www.spammarshal l.com>

    ------------------------------------------------------------------------


    Comment

    • hiwa

      #3
      Re: How to get the line number in the code?

      Kathy Benson <fornewsgroups@ yahoo.com> wrote in message news:<3F7C995C. 90602@yahoo.com >...[color=blue]
      > You cannot do that in Java. The only way to get line number is by
      > creating a Throwable object and then extracting the stack trace. You
      > cannot find the line number at compile time.
      >
      > Regards,
      > KB
      >
      >
      >
      >
      >
      > Charles wrote:
      >[color=green]
      > >Is there a way to obtain the line number in my code? For example:
      > >1 public class MyClass {
      > >2 int a = 5;
      > >3 System.out.prin tln("Integer variable a was set on line number: " + x);
      > >4 etc.
      > >Where x is equal to '2'.
      > >
      > >How can I obtain 'x' ?
      > >
      > >Thanks.
      > >
      > >
      > >
      > >[/color]
      >
      > --
      > ------------------------------------------------------------------------
      > This email is certified to be Spam free by Spam Marshall
      >
      > You don't pay to get spam, why pay to clean it?
      > Visit http://www.spammarshall.com to create an account for free
      > <http://www.spammarshal l.com>
      >
      > ------------------------------------------------------------------------
      >
      >
      > --[/color]

      You can use java.io.LineNum berReader of LineNumberInput Stream. Both
      class has getLineNumber() method.

      Comment

      • Bryce (Work)

        #4
        Re: How to get the line number in the code?

        On 2 Oct 2003 20:32:13 -0700, HGA03630@nifty. ne.jp (hiwa) wrote:
        [color=blue][color=green][color=darkred]
        >> >Is there a way to obtain the line number in my code? For example:
        >> >1 public class MyClass {
        >> >2 int a = 5;
        >> >3 System.out.prin tln("Integer variable a was set on line number: " + x);
        >> >4 etc.
        >> >Where x is equal to '2'.
        >> >
        >> >How can I obtain 'x' ?[/color][/color]
        >
        >You can use java.io.LineNum berReader of LineNumberInput Stream. Both
        >class has getLineNumber() method.[/color]

        Actually, the original poster wanted to do this at runtime. I suppose
        you could do some fiddling by reading the .java file into
        LineNumberReade r, and locating the code you were executing.

        Comment

        • Charles

          #5
          Re: How to get the line number in the code?

          Correct, I wish to be able to do this at runtime. I was hoping for a quick
          and easy way to do this with minimum overhead.

          "Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
          news:ik0rnv0eq9 orokfhniq29l43q emfirrkta@4ax.c om...[color=blue]
          > On 2 Oct 2003 20:32:13 -0700, HGA03630@nifty. ne.jp (hiwa) wrote:
          >[color=green][color=darkred]
          > >> >Is there a way to obtain the line number in my code? For example:
          > >> >1 public class MyClass {
          > >> >2 int a = 5;
          > >> >3 System.out.prin tln("Integer variable a was set on line number: "[/color][/color][/color]
          + x);[color=blue][color=green][color=darkred]
          > >> >4 etc.
          > >> >Where x is equal to '2'.
          > >> >
          > >> >How can I obtain 'x' ?[/color]
          > >
          > >You can use java.io.LineNum berReader of LineNumberInput Stream. Both
          > >class has getLineNumber() method.[/color]
          >
          > Actually, the original poster wanted to do this at runtime. I suppose
          > you could do some fiddling by reading the .java file into
          > LineNumberReade r, and locating the code you were executing.[/color]


          Comment

          • Bryce (Work)

            #6
            Re: How to get the line number in the code?

            On Fri, 3 Oct 2003 11:34:01 -0400, "Charles" <csw@rogers.com > wrote:
            [color=blue]
            >Correct, I wish to be able to do this at runtime. I was hoping for a quick
            >and easy way to do this with minimum overhead.[/color]

            Then the answer is no.
            [color=blue]
            >"Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
            >news:ik0rnv0eq 9orokfhniq29l43 qemfirrkta@4ax. com...[color=green]
            >> On 2 Oct 2003 20:32:13 -0700, HGA03630@nifty. ne.jp (hiwa) wrote:
            >>[color=darkred]
            >> >> >Is there a way to obtain the line number in my code? For example:
            >> >> >1 public class MyClass {
            >> >> >2 int a = 5;
            >> >> >3 System.out.prin tln("Integer variable a was set on line number: "[/color][/color]
            >+ x);[color=green][color=darkred]
            >> >> >4 etc.
            >> >> >Where x is equal to '2'.
            >> >> >
            >> >> >How can I obtain 'x' ?
            >> >
            >> >You can use java.io.LineNum berReader of LineNumberInput Stream. Both
            >> >class has getLineNumber() method.[/color]
            >>
            >> Actually, the original poster wanted to do this at runtime. I suppose
            >> you could do some fiddling by reading the .java file into
            >> LineNumberReade r, and locating the code you were executing.[/color]
            >[/color]

            Comment

            • Raymond DeCampo

              #7
              Re: How to get the line number in the code?

              Charles wrote:[color=blue]
              > Correct, I wish to be able to do this at runtime. I was hoping for a quick
              > and easy way to do this with minimum overhead.
              >[/color]

              [Java purists look away.]
              The easiest way would be to use the C precompiler. Or you could even
              write your own precompiler (perhaps even in Java, to satisfy the purists).

              Ray

              Comment

              • jmgonzal
                New Member
                • Mar 2006
                • 1

                #8
                Originally posted by Raymond DeCampo
                Charles wrote:[color=blue]
                > Correct, I wish to be able to do this at runtime. I was hoping for a quick
                > and easy way to do this with minimum overhead.
                >[/color]

                [Java purists look away.]
                The easiest way would be to use the C precompiler. Or you could even
                write your own precompiler (perhaps even in Java, to satisfy the purists).

                Ray
                ------------------
                This is the answer for jdk 1.4


                String linenumber = new Exception().get StackTrace()[0].getLineNumber( );
                System.out.prin tln("The line number is " + linenumber);

                Comment

                Working...