0

Similar questions have been asked in the past e.g. whether Visual Studio has any capability to visualise the line number a C++ program is executing, although I'm wondering if it might be possible to say, print out the src code line number that a Java application's main thread is on (not IDE-specific). I haven't a scooby where to start - having googled a little, I'm struggling to find useful results.

  • 1
    No idea either (maybe you can do some tricks by throwing and catching an exception and manipulating the stacktrace?). But I'm curious to know *why* you need this. – Federico klez Culloca Feb 21 '22 at 16:15
  • A Java program almost always is running multiple threads. Therefore there is no singular "line it's executing". But you can [get a stack dump](https://stackoverflow.com/questions/10756105/how-to-get-a-complete-stack-trace-of-a-running-java-program-that-is-taking-100/10756126) of any given Java program using `jstack`. That dump will give you a detailed view of what each thread is currently doing. – Joachim Sauer Feb 21 '22 at 16:16
  • Given that a Java program can be written as a single line of source code, it should be obvious how little running Java programs technically care about line numbers. You can continually look at the current thread's stack and printing corresponding line numbers, but you'll have quite a hard time linking "statements" to line numbers, and you might have to rewrite your code just for this (make it Python-like). The question really is what you think you need this for. – ernest_k Feb 21 '22 at 16:17
  • @JoachimSauer Thanks, I'll check out jstack - note, I'm only asking about the "main" thread – WobbleMeister Feb 21 '22 at 16:32
  • @ernest_k I am assessing the feasibility of writing an application that can visualise in the editor the line of code the main thread is executing. The code it would be operating on would be very basic Java programs. I guess, if multiple statements were written on a single line, I would just output that line. The user would have to separate out their code if they wanted clarity about what's currently being executed. – WobbleMeister Feb 21 '22 at 16:43

0 Answers0