Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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: 24204
44
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
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic