,可以通过以下步骤实现:
以下是一个示例代码:
public class ClassNameExample {
public static void main(String[] args) {
// 获取当前线程的堆栈轨迹信息
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
// 遍历堆栈轨迹信息数组
for (StackTraceElement element : stackTrace) {
// 找到包含目标类名的StackTraceElement对象
if (element.getClassName().contains("目标类名")) {
// 获取完整的类名
String fullClassName = element.getClassName();
// 截取不带包的类名
String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
System.out.println("不带包的类名:" + className);
break;
}
}
}
}
这样,你就可以通过以上代码从StackTraceElement获取不带包的类名了。
注意:以上示例代码中的"目标类名"需要替换为你实际需要获取的类名。
领取专属 10元无门槛券
手把手带您无忧上云