码迷,mamicode.com
首页 > 编程语言 > 详细

java如何寻找main函数对应的类

时间:2018-01-23 10:50:40      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:技术   ati   com   tac   blog   and   trace   .com   spring   

参考springboot 

Class<?> deduceMainApplicationClass() {
    try {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if ("main".equals(stackTraceElement.getMethodName())) {
                return Class.forName(stackTraceElement.getClassName());
            }
        }
    }
    catch (ClassNotFoundException ex) {
        // Swallow and continue
    }
    return null;
}

 

技术分享图片

 

java如何寻找main函数对应的类

标签:技术   ati   com   tac   blog   and   trace   .com   spring   

原文地址:https://www.cnblogs.com/hujunzheng/p/8333955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!