472,356 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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 25843
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
5
by: Mark Kamoski | last post by:
Hi Everyone-- How can one get the line number of where an error was thrown and/or caught? For example, note the following, for use at any given point in a piece of code: ....to get the...
10
by: Al | last post by:
Hi, I am seeking to obtain the line number of an error once deployed with out inserting line numbers in the method. The IDE inserts line numbers but this function does not appear to be a...
3
by: Rotsey | last post by:
Hi, I am trying to write some error handling code to log the error. But getting the line number of the error is not simple. Is it because the app is set to "Release" and does not have debug...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
5
by: IdleBrain | last post by:
I am trying to log the Application name, Method name, line number and column number whenever an exception is generated in an C# 2005 application using the following code. Problem is that the...
2
by: =?Utf-8?B?QXJ0aHVyIFBhcmtlcg==?= | last post by:
I've been having a problem occasionally with exceptions having the wrong line number listed. The situation is this: I have a function with a try-catch, and in the catch it logs the message and...
11
by: Horacius ReX | last post by:
Hi, I have to search for a string on a big file. Once this string is found, I would need to get the number of the line in which the string is located on the file. Do you know how if this is...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.