Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6600615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:38:51+00:00 2026-05-25T18:38:51+00:00

Is it possible, given a java.lang.Class object, to get the source file name and

  • 0

Is it possible, given a java.lang.Class object, to get the source file name and the line number at which the class was declared?

The data should be available in the .class file’s debug info. The only place I know of, where the JDK returns such debug info is in java.lang.StackTraceElement but I’m not sure if it’s possible to force Java to create a java.lang.StackTraceElement instance for an arbitrary class, because we are not executing a method in the class.

My exact use case is an anonymous inner class which has a compiler generated name. I want to know the file name and the line number for the class declaration.

I prefer not to use a byte-code manipulation framework, but I can fall back to it if I have to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:38:52+00:00Added an answer on May 25, 2026 at 6:38 pm

    The answer to this comes down to how much control you have over the code that is implementing the Listener. You are right that it is not possible to create a stacktrace without being in a method.

    The general technique is to create an Exception(), in the constructor, but don’t throw it. This contains the stacktrace information, which you can use how you want. This will give you the line number of the constructor, but not of the class. Please note that this method is not particularly performant either, because creating a stacktrace is expensive.

    You will need to either:

    1. force some code to be executed in the constructor (relatively easy if your Listener is an abstract class which you control)
    2. Instrument the code somehow (the cure seems worse than the disease here).
    3. Make some assumptions about the way classes are named.
    4. Read the jar (do the same thing as javac -p)

    For 1), you’d simply put the Exception creation in the abstract class, and the constructor gets called by the subclass:

    class Top {
        Top() {
            new Exception().printStackTrace(System.out);
        }
    }
    
    class Bottom extends Top {
        public static void main(String[] args) {
            new Bottom();
        }
    }
    

    this produces something like:

    java.lang.Exception
        at uk.co.farwell.stackoverflow.Top.<init>(Top.java:4)
        at uk.co.farwell.stackoverflow.Bottom.<init>(Bottom.java: 11)
        at uk.co.farwell.stackoverflow.Bottom.main(Bottom.java: 18)
    

    In general, there are some naming rules which are followed: If you have an outer class called Actor and an inner called Consumer, then the compiled class will be called Actor$Consumer. Anonymous inner classes are named in the order in which they appear in the file, so Actor$1 will appear in the file before Actor$2. I don’t think this is actually specified anywhere, so this is probably just a convention, and shouldn’t be relied upon if you’re doing anything sophisticated with multiple jvms etc.

    It is possible, as jmg pointed out, that you can define multiple top level classes in the same file. If you have a public class Foo, this must be defined in Foo.java, but a non-public class can be included in another file. The above method will cope with this.

    Explanation:

    If you disassemble the java (javap -c -verbose), you’ll see that there are line numbers in the debug information, but they only apply to methods. Using the following inner class:

    static class Consumer implements Runnable {
        public void run() {
            // stuff
        }
    }
    

    and the javap output contains:

    uk.co.farwell.stackoverflow.Actors$Consumer();
      Code:
       Stack=1, Locals=1, Args_size=1
       0:   aload_0
       1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
       4:   return
      LineNumberTable: 
       line 20: 0
    
      LocalVariableTable: 
       Start  Length  Slot  Name   Signature
       0      5      0    this       Luk/co/farwell/stackoverflow/Actors$Consumer;
    

    The LineNumberTable contains the list of line numbers which apply to a method. So my constructor for the Consumer starts at line 20. But this is the first line of the constructor, not the first line of the class. It is only the same line because I’m using the default constructor. If I add a constructor, then the line numbers will change. the compiler does not store the line that the class is declared on. So you can’t find where the class is declared without parsing the java itself. You simply don’t have the information available.

    However, if you’re using an anonymous inner class such as:

    Runnable run = new Runnable() {
        public void run() {
        }
    };
    

    Then the line number of the constructor and class will match[*], so this gives you an line number.

    [*] Except if the “new” and “Runnable()” are on different lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a class instance, is it possible to determine if it implements a particular
Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
In Java, is it possible to associate some object (i.e. a String) with a
Given a handle of type HWND is it possible to confirm that the handle
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
I'm a newbie in Java so I'm not sure if this is possible. Basically
I have to write an application in Java EE6. I have been given a
Is it possible to test for the existence of other references to an object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.