$0 (Program Name) in Java? Discover main class?

前端 未结 8 946
死守一世寂寞
死守一世寂寞 2020-12-03 00:23

Is there a way to find the name of the program that is running in Java? The class of the main method would be good enough.

相关标签:
8条回答
  • 2020-12-03 00:53

    This is the code I came up with when using the combined responses of jodonnell and John Meagher. It stores the main class in a static variable to reduce overhead of repeated calls:

    private static Class<?> mainClass;
    
    public static Class<?> getMainClass() {
      if (mainClass != null)
        return mainClass;
    
      Collection<StackTraceElement[]> stacks = Thread.getAllStackTraces().values();
      for (StackTraceElement[] currStack : stacks) {
        if (currStack.length==0)
          continue;
        StackTraceElement lastElem = currStack[currStack.length - 1];
        if (lastElem.getMethodName().equals("main")) {
          try {
            String mainClassName = lastElem.getClassName();
            mainClass = Class.forName(mainClassName);
            return mainClass;
          } catch (ClassNotFoundException e) {
            // bad class name in line containing main?! 
            // shouldn't happen
            e.printStackTrace();
          }
        }
      }
      return null;
    }
    
    0 讨论(0)
  • 2020-12-03 00:57

    Or you could just use getClass(). You can do something like:

    public class Foo
    {
        public static final String PROGNAME = new Foo().getClass().getName();
    }
    

    And then PROGNAME will be available anywhere inside Foo. If you're not in a static context, it gets easier as you could use this:

    String myProgramName = this.getClass().getName();
    
    0 讨论(0)
  • 2020-12-03 01:00

    Try this:

        StackTraceElement[] stack = Thread.currentThread ().getStackTrace ();
        StackTraceElement main = stack[stack.length - 1];
        String mainClass = main.getClassName ();
    

    Of course, this only works if you're running from the main thread. Unfortunately I don't think there's a system property you can query to find this out.

    Edit: Pulling in @John Meagher's comment, which is a great idea:

    To expand on @jodonnell you can also get all stack traces in the system using Thread.getAllStackTraces(). From this you can search all the stack traces for the "main" Thread to determine what the main class is. This will work even if your class is not running in the main thread.

    0 讨论(0)
  • 2020-12-03 01:06

    Also from the command line you could run the jps tool. Sounds like a

    jps -l 
    

    will get you what you want.

    0 讨论(0)
  • 2020-12-03 01:08

    Try this :

    Java classes have static instance of their own class (java.lang.Class type).

    That means if we have a class named Main. Then we can get its class instance by Main.class

    If you're interested in name only then,

    String className = Main.class.getName();

    0 讨论(0)
  • 2020-12-03 01:09
    System.getProperty("sun.java.command")
    
    0 讨论(0)
提交回复
热议问题