Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this possible?

 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Murali Mohan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic