• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this possible?

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic