8 Replies Latest reply: Feb 16, 2009 10:18 PM by baftos RSS

    Can a main method know what class it’s being called from/inside.

    807588
      Basically I have an abstract class with a normal 'public static void main(String[] args)' and inside it I'd like to instantiate the class that this main is called within. For example:

      File 1:
      Public abstract MyAbstractClass {
      
        Public abstract void printIt();
      
        Public static void main(String[] args) {
          MyAbstractClass handle = ??<insert something creative here>??;
          handle.printIt();
        }
      }
      File 2:
      Public Test extends MyAbstractClass {
      
        Public Test(){
        }
      
        Public void printIt() {
          System.out.println("hello world");
        }
      }
      This code is executed by the command 'java Test' (no parameters) and I'd like to call the non-static printIt() function. (This is a simple example, in my case the methods in the class actually do stuff and can't be static).

      I've gotten the stack trace(s) in the main, but it only references the abstract class. I've looked/iterated through the environment and the system properties and I can't find any references to the 'Test' class. I'm currently trying to find a way to get the command-line that was executed (AKA 'java Test' in this example), but right now that's coming up empty too. I know if I can get the package.className I can use reflection to instantiate the class, I just can't find the information programmatically.

      I know this is kind of crazy, but does anyone have any ideas?
        • 1. Re: Can a main method know what class it’s being called from/inside.
          807588
          veryhidden wrote:
          Basically I have an abstract class with a normal 'public static void main(String[] args)'
          Why?
          and inside it I'd like to instantiate the class that this main is called within.
          No.

          What are you trying to do here? What you are currently trying is a bad idea which is why it won't work.
          • 2. Re: Can a main method know what class it’s being called from/inside.
            807588
            Also are you sure you're programming in Java and not something else? Because none of what you posted will compile..
            • 3. Re: Can a main method know what class it’s being called from/inside.
              807588
              One way to do what I think you want to accomplish is to pass the name of the class you want to create to the main method through the command line, .e.g. 'java ClassWithMain -class ImplementingClassName'.
              And then do Class.forName(ImplementingClassName) casting the results to your abstract class. It would probably be clearer though to have ClassWithMain not be the abstract parent of the implementor, but instead some kind of controller that starts whatever you're trying to do.
              • 4. Re: Can a main method know what class it’s being called from/inside.
                baftos
                Brynjar wrote:
                One way to do what I think you want to accomplish is to pass the name of the class you want to create to the main method through the command line, .e.g. 'java ClassWithMain -class ImplementingClassName'.
                RuntimeMXBean.getInputArguments() does the trick, but, as others pointed out, it is just a trick.
                With a proper design, you most probably should not need to do this.
                • 5. Re: Can a main method know what class it’s being called from/inside.
                  807588
                  baftos wrote:
                  RuntimeMXBean.getInputArguments() does the trick, but, as others pointed out, it is just a trick.
                  With a proper design, you most probably should not need to do this.
                  Yes, just one trick of many to decide what class should implement some functionality at runtime rather than compile time. But an easy one anyway.
                  • 6. Re: Can a main method know what class it’s being called from/inside.
                    807588
                    Sorry i forgot to add the class keyword to the class definitions and my editor accidentally capitalized the 'public's before the methods and constructors. Fix those problems and add null for being creative (GRIN) then it will compile with suns javac on my linux box.
                    • 7. Re: Can a main method know what class it’s being called from/inside.
                      807588
                      In main when i run:
                      java.util.List<String> data = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments();
                      System.out.println(data.size());
                      i get a zero for the size. Am i missing something?
                      i tried running the command
                      java -Dfile.encoding=UTF-8 Test
                      and got the list with one element being the '-Dfile.encoding=UTF-8' string.


                      This isn't for final code (i agree it's not a good design), I'm trying to write some code for test-cases/debug stuff. With my current configuration and team compliment this is the best option, plus now it's a personal challenge. :-)
                      • 8. Re: Can a main method know what class it’s being called from/inside.
                        baftos
                        veryhidden wrote:
                        In main when i run:
                        java.util.List<String> data = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments();
                        System.out.println(data.size());
                        i get a zero for the size. Am i missing something?
                        i tried running the command
                        java -Dfile.encoding=UTF-8 Test
                        and got the list with one element being the '-Dfile.encoding=UTF-8' string.
                        If that's the case, sorry for missleading you. I thought this includes the class containing main(), shouldn't it?
                        Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method.
                        But anyway, I think the previous poster's idea is better because:
                        1. Does not depend on how the program starts, like from an executable jar.
                        2. It works:)