<p>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.</p>

[{'answer_id': 41904, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 7, 'selected': True, 'text': '<p>Try this:</p>\n\n<pre><code>    StackTraceElement[] stack = Thread.currentThread ().getStackTrace ();\n    StackTraceElement main = stack[stack.length - 1];\n    String mainClass = main.getClassName ();\n</code></pre>\n\n<p>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.</p>\n\n<p><strong>Edit:</strong> Pulling in @John Meagher\'s comment, which is a great idea:</p>\n\n<blockquote>\n  <p>To expand on @jodonnell you can also\n  get all stack traces in the system\n  using Thread.getAllStackTraces(). From\n  this you can search all the stack\n  traces for the "main" Thread to\n  determine what the main class is. This\n  will work even if your class is not\n  running in the main thread.</p>\n</blockquote>\n'}, {'answer_id': 42082, 'author': 'polarbear', 'author_id': 3636, 'author_profile': 'https://Stackoverflow.com/users/3636', 'pm_score': 2, 'selected': False, 'text': '<p>Also from the command line you could run the <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jps.html" rel="nofollow noreferrer">jps</a> tool. Sounds like a </p>\n\n<pre><code>jps -l \n</code></pre>\n\n<p>will get you what you want.</p>\n'}, {'answer_id': 42938, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 4, 'selected': False, 'text': '<p>To expand on @jodonnell you can also get all stack traces in the system using <a href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#getAllStackTraces%28%29" rel="nofollow noreferrer">Thread.getAllStackTraces()</a>. From this you can search all the stack traces for the <code>main</code> Thread to determine what the main class is. This will work even if your class is not running in the main thread.  </p>\n'}, {'answer_id': 2421152, 'author': 'Adam', 'author_id': 291006, 'author_profile': 'https://Stackoverflow.com/users/291006', 'pm_score': -1, 'selected': False, 'text': "<p>Or you could just use getClass().  You can do something like:</p>\n\n<pre><code>public class Foo\n{\n    public static final String PROGNAME = new Foo().getClass().getName();\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>String myProgramName = this.getClass().getName();\n</code></pre>\n"}, {'answer_id': 7201901, 'author': 'color', 'author_id': 913678, 'author_profile': 'https://Stackoverflow.com/users/913678', 'pm_score': -1, 'selected': False, 'text': "<p>Try this :</p>\n\n<p>Java classes have static instance of their own class (java.lang.Class type).</p>\n\n<p>That means if we have a class named Main.\nThen we can get its class instance by\nMain.class</p>\n\n<p>If you're interested in name only then,</p>\n\n<p>String className = Main.class.getName();</p>\n"}, {'answer_id': 8883260, 'author': 'tschodt', 'author_id': 1069589, 'author_profile': 'https://Stackoverflow.com/users/1069589', 'pm_score': 2, 'selected': False, 'text': '<p>For access to the class objects when you are in a static context</p>\n\n<pre><code>public final class ClassUtils {\n    public static final Class[] getClassContext() {\n        return new SecurityManager() { \n            protected Class[] getClassContext(){return super.getClassContext();}\n        }.getClassContext(); \n    };\n    private ClassUtils() {};\n    public static final Class getMyClass() { return getClassContext()[2];}\n    public static final Class getCallingClass() { return getClassContext()[3];}\n    public static final Class getMainClass() { \n        Class[] c = getClassContext();\n        return c[c.length-1];\n    }\n    public static final void main(final String[] arg) {\n        System.out.println(getMyClass());\n        System.out.println(getCallingClass());\n        System.out.println(getMainClass());\n    }\n}\n</code></pre>\n\n<p>Obviously here all 3 calls will return </p>\n\n<pre><code>class ClassUtils\n</code></pre>\n\n<p>but you get the picture;</p>\n\n<pre><code>classcontext[0] is the securitymanager\nclasscontext[1] is the anonymous securitymanager\nclasscontext[2] is the class with this funky getclasscontext method\nclasscontext[3] is the calling class\nclasscontext[last entry] is the root class of this thread.\n</code></pre>\n'}, {'answer_id': 12031392, 'author': 'Andrew Taylor', 'author_id': 303442, 'author_profile': 'https://Stackoverflow.com/users/303442', 'pm_score': 3, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>private static Class&lt;?&gt; mainClass;\n\npublic static Class&lt;?&gt; getMainClass() {\n  if (mainClass != null)\n    return mainClass;\n\n  Collection&lt;StackTraceElement[]&gt; stacks = Thread.getAllStackTraces().values();\n  for (StackTraceElement[] currStack : stacks) {\n    if (currStack.length==0)\n      continue;\n    StackTraceElement lastElem = currStack[currStack.length - 1];\n    if (lastElem.getMethodName().equals("main")) {\n      try {\n        String mainClassName = lastElem.getClassName();\n        mainClass = Class.forName(mainClassName);\n        return mainClass;\n      } catch (ClassNotFoundException e) {\n        // bad class name in line containing main?! \n        // shouldn\'t happen\n        e.printStackTrace();\n      }\n    }\n  }\n  return null;\n}\n</code></pre>\n'}, {'answer_id': 12989117, 'author': 'Nadav Brandes', 'author_id': 1622468, 'author_profile': 'https://Stackoverflow.com/users/1622468', 'pm_score': 4, 'selected': False, 'text': '<pre><code>System.getProperty("sun.java.command")\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/41894', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/823/']