473,699 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 17 '05 #1
7 25970
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.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.


--
------------------------------------------------------------------------
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>

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


--
------------------------------------------------------------------------
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>

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


You can use java.io.LineNum berReader of LineNumberInput Stream. 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.prin tln("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.LineNum berReader of LineNumberInput Stream. 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
LineNumberReade r, 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******@berze rker-soft.com> wrote in message
news:ik******** *************** *********@4ax.c om...
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.prin tln("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.LineNum berReader of LineNumberInput Stream. 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
LineNumberReade r, 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******@berze rker-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.prin tln("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.LineNum berReader of LineNumberInput Stream. 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
LineNumberReade r, 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
jmgonzal
1 New Member
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);
Mar 31 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
21922
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 C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
5
1946
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 current Assembly's name, one can use this... System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
10
5247
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 deployment option. Without entering a line number eg.(10210:) in the code erl and e.tostring do not indicate the line on which the error occured. Inserting line numbers appeares to insert no-ops into the code when it is stepped through in the IDE. Is...
3
1856
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 info. Is there a way of getting the line number when in release mode????? Thanks
4
3003
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") _prtSKU = myDLReader.GetString("prtSKU") _prtPic = myDLReader.GetString("prtPic") _prtRsvQty = myDLReader.GetInteger("prtRsvQty")
5
4485
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 line number that is being obtained by stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage function's line number. I am trying to write the line number of the statement which is causing an exception and not the LogMessage function's line...
2
3196
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 stack trace from the exception. The code generates an index-out-of-bounds exception. When I look at the line number listed in the stack trace, it is the first line of code in the function, which has nothing to do with array indexes. One time this...
11
21173
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 possible to do in python ? Thanks
4
3950
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.... ................... ..
0
9187
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.