wood burning stoves 2.0*
The moose likes Java in General and the fly likes Is this possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 3502
    
  13
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

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: 3502
    
  13
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.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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 - Every great individual common man
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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://aspose.com/file-tools
 
subject: Is this possible?