469,267 Members | 1,012 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.
Jul 17 '05 #1
7 25625
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:
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.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.


--
------------------------------------------------------------------------
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.spammarshall.com>

------------------------------------------------------------------------
Jul 17 '05 #2
Kathy Benson <fo***********@yahoo.com> wrote in message news:<3F************@yahoo.com>...
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:
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.println("Integer variable a was set on line number: " + x);
4 etc.
Where x is equal to '2'.

How can I obtain 'x' ?

Thanks.


--
------------------------------------------------------------------------
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.spammarshall.com>

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


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.
Jul 17 '05 #3
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>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.println("Integer variable a was set on line number: " + x);
>4 etc.
>Where x is equal to '2'.
>
>How can I obtain 'x' ?


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.


Actually, the original poster wanted to do this at runtime. I suppose
you could do some fiddling by reading the .java file into
LineNumberReader, and locating the code you were executing.
Jul 17 '05 #4
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)" <sp******@berzerker-soft.com> wrote in message
news:ik********************************@4ax.com...
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>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.println("Integer variable a was set on line number: " + x); >4 etc.
>Where x is equal to '2'.
>
>How can I obtain 'x' ?


You can use java.io.LineNumberReader of LineNumberInputStream. Both
class has getLineNumber() method.


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

Jul 17 '05 #5
On Fri, 3 Oct 2003 11:34:01 -0400, "Charles" <cs*@rogers.com> wrote:
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.
Then the answer is no.
"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:ik********************************@4ax.com.. .
On 2 Oct 2003 20:32:13 -0700, HG******@nifty.ne.jp (hiwa) wrote:
>> >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.println("Integer variable a was set on line number: "+ x); >> >4 etc.
>> >Where x is equal to '2'.
>> >
>> >How can I obtain 'x' ?
>
>You can use java.io.LineNumberReader of LineNumberInputStream. Both
>class has getLineNumber() method.


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


Jul 17 '05 #6
Charles wrote:
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.


[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

Jul 17 '05 #7
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().getStackTrace()[0].getLineNumber();
System.out.println("The line number is " + linenumber);
Mar 31 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Rotsey | last post: by
4 posts views Thread by Jeff Jarrell | last post: by
5 posts views Thread by IdleBrain | last post: by
2 posts views Thread by =?Utf-8?B?QXJ0aHVyIFBhcmtlcg==?= | last post: by
11 posts views Thread by Horacius ReX | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.