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 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 510k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Client Side For any program that needs to poll the… May 16, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer Sorry, I don't quite get what columns you are joining… May 16, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer The /WEB-INF/classes is just part of the classpath. You could… May 16, 2026 at 5:09 pm

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 have a Java program that auto dials phone numbers, it can generate sounds
I have a very limited knowledge on AS400 and RPG. But we have a
I have a Java application which needs to be audited (so obviously I need
I am trying to write a program in java where in i can find
I have a java CLI script that converts rgb names to hexcodes (e.g. 144
I have a problem with some JNI code. I'm getting the following error printed
I've searched around but haven't quite found what I'm looking for. In a nutshell
this might be a bit long so my apologies. consider the following code (i've
Due to comments I added the following code (in BasicPanel) Connect(CTRL_ONE, wxEVT_KILL_FOCUS, (wxObjectEventFunction)&BasicPanel::OnKillFocus); Connect(CTRL_TWO,wxEVT_KILL_FOCUS,
my program needs to take a prepopulated sql database and then save the records

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.