This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Is this possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is this possible?" Watch "Is this possible?" New topic
Author

Is this possible?

A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
It it possible to get the current source code line number in order to output is as follows?

System.out.println("debug line output @ line no = " + lineNo);

thanks in advance!

harry
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
If you print the stack trace of an Exception, line numbers are included, so it would appear it is possible. Try looking at the source code for the Exception class to see how it is done there.
The JDK installation includes the source code in a file called src.zip.


Joanne
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66
I will tell you a naughty way to do this.
in a try block throw an exception. Then catch that there only. then parse the stack trace to get the line number. Oh now you got the line number. Do whatever you want ( +2 , -2 )

How is it?


Thanks,<br />Murali...
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

Yes, it is possible, but you should not try to invent your own logging framework.

Why not use a popular existing logging package such as Log4J, which you can configure to do this automatically?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Originally posted by Murali Mohan:
I will tell you a naughty way to do this.
in a try block throw an exception. Then catch that there only. then parse the stack trace to get the line number. Oh now you got the line number. Do whatever you want ( +2 , -2 )

How is it?


You don't actually need to throw and catch the exception. Just create one and you can then retrieve the stack trace from it.
Ankur Sharma
Ranch Hand

Joined: Dec 27, 2005
Posts: 1234
Originally posted by A Harry:
It it possible to get the current source code line number in order to output is as follows?

System.out.println("debug line output @ line no = " + lineNo);

thanks in advance!

harry


Here is an example:



And also the output is.

stck[1]:= 16
stck[2]:= 5



Hope it helps you out..


The Best way to predict your future is to create it
Ankur Sharma
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Note that in recent versions of Java, the exception object isn't needed. Just call Thread.currentThread().getStackTrace() to get an array of StackTraceElement objects.


[Jess in Action][AskingGoodQuestions]
Ankur Sharma
Ranch Hand

Joined: Dec 27, 2005
Posts: 1234
Originally posted by Ernest Friedman-Hill:
Note that in recent versions of Java, the exception object isn't needed. Just call Thread.currentThread().getStackTrace() to get an array of StackTraceElement objects.


I don't think so is it possible in JDK 1.4
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Is this possible?
 
Similar Threads
allowable values for case labels ?
Cannot forward. Writer or Stream already obtained
about bitwise OR
SQLException: ResultSet Exhuasted
Head First 2nd edition Code Magnets