I want to determine the class name where my application started, the one with the main() method, at runtime, but I\'m in another thread and my stacktrace doesn\'t go all the
Another way to get main class is look for that class on Thread.getAllStackTraces, so you could find it even inside jars, it works on any SDK (Open, Oracle...):
private static Class> mainClass = null;
public static Class> getMainClass()
{
if (mainClass == null)
{
Map threadSet = Thread.getAllStackTraces();
for (Map.Entry entry : threadSet.entrySet())
{
for (StackTraceElement stack : entry.getValue())
{
try
{
String stackClass = stack.getClassName();
if (stackClass != null && stackClass.indexOf("$") > 0)
{
stackClass = stackClass.substring(0, stackClass.lastIndexOf("$"));
}
Class> instance = Class.forName(stackClass);
Method method = instance.getDeclaredMethod("main", new Class[]
{
String[].class
});
if (Modifier.isStatic(method.getModifiers()))
{
mainClass = instance;
break;
}
}
catch (Exception ex)
{
}
}
}
return mainClass;
}
}