且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中获取当前类名,包括包名?

更新时间:2023-02-26 10:50:42

使用 this.getClass().getCanonicalName() 获取完整的类名.

Use this.getClass().getCanonicalName() to get the full class name.

请注意,包/类名 ("abC") 与 .class 文件 (a/b/C.class) 的路径不同,使用包名/类名派生路径通常是不好的做法.类文件/包的集合可以在多个不同的类路径中,可以是目录,也可以是jar文件.

Note that a package / class name ("a.b.C") is different from the path of the .class files (a/b/C.class), and that using the package name / class name to derive a path is typically bad practice. Sets of class files / packages can be in multiple different class paths, which can be directories or jar files.