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

  • Home
  • SEARCH
  • 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 1032449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:04:42+00:00 2026-05-16T14:04:42+00:00

I have found following question: $0 (Program Name) in Java? Discover main class? but

  • 0

I have found following question:
$0 (Program Name) in Java? Discover main class?
but accepted answer fails in this situation:

public class Derived extends Base { }

class Base {
    public static void main(String[] args){
        System.out.println(getTheClassName());
    }

    static String getTheClassName(){
        StackTraceElement[] stack = Thread.currentThread().getStackTrace();
        StackTraceElement main = stack[stack.length - 1];
        return main.getClassName();
    }
}

When calling:

java Derived

the output is unfortunately Base not Derived as expected.

Is it possible to get the name of real main class? (btw. jps tool detects main class correctly but to get the proper name I need to know ID of current VM, and that is also a problem)

Best regards.

Edit: to be precise: I need to know what is the name of class passed as command line argument passed to JVM. jps tools does it the way I want, but I cannot use this tool because of other problems (such as unknown VMid).

Edit2: replying globally to Thorbjørn’s answer:

For your approach to work, you need to have a non-static method instead. Make it
non-static, have a main() in Derived doing a new Derived().getTheClassName()

I am wondering I can get the name without implementing main method in Derived class.

Solution: I will provide both (Thorbjørn’s and erickson’s) mechanisms of an initialization of Derived class 😉 All of the answers were very helpful. I accepted Thorbjørn’s becase he posted his answer earlier than erickson. Thank You.

  • 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-16T14:04:43+00:00Added an answer on May 16, 2026 at 2:04 pm

    Static methods belong to the class they are defined in, and not the instance of the class (same as static/non-static methods). Hence when you are inside getTheClassName you are in a static method defined in the Base class.

    For your approach to work, you need to have a non-static method instead. Make it non-static, have a main() in Derived doing a new Derived().getTheClassName()

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

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 544k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer .pfx files tend to be PKCS#12 files (which will contain… May 17, 2026 at 8:02 am
  • Editorial Team
    Editorial Team added an answer I believe a custom import method is what you're after:… May 17, 2026 at 8:01 am
  • Editorial Team
    Editorial Team added an answer take a look at this link: http://www.machoe.com/1602/enhance-your-apple-remote-with-mira.html i haven't tried… May 17, 2026 at 8:01 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm converting an application to use Java 1.5 and have found the following method:
I've found the following blog http://www.modalitysystems.com/blog/?cat=14 but have not had the time to delve
According to what I have found so far, I can use the following code:
I found the following rather strange. Then again, I have mostly used closures in
So: I have the following function, adapted from a formula found online, which takes
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I do not know if this the correct forum to ask this question but
I have a winform data entry and retrieval program called CaseNotes. This has a
I have found that my HTML is, to be honest, very clunky. Small, simple
I have found some libraries or web services in PHP that does the job.

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.