如何在Java中获取当前类名包括包名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9729197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current class name including package name in Java?
提问by La bla bla
I'm working on a project and one requirement is if the 2nd argument for the main method starts with “/
” (for linux) it should consider it as an absolute path (not a problem), but if it doesn't start with “/
”, it should get the current working pathof the class and append to it the given argument.
我正在做一个项目,一个要求是如果 main 方法的第二个参数以“ /
”开头(对于 linux),它应该将其视为绝对路径(不是问题),但如果它不以“开头” /
”,它应该获取类的当前工作路径并将给定的参数附加到它。
I can get the class name in several ways: System.getProperty("java.class.path")
, new File(".")
and getCanonicalPath()
, and so on...
我可以通过多种方式获取类名:System.getProperty("java.class.path")
、new File(".")
和getCanonicalPath()
,等等...
The problem is, this only gives me the directory in which the packages are stored - i.e. if I have a class stored in ".../project/this/is/package/name
", it would only give me "/project/
" and ignores the package name where the actual .class files
lives.
问题是,这只会给我存储包的目录 - 即如果我有一个存储在“ .../project/this/is/package/name
”中的类,它只会给我“ /project/
”并忽略实际所在的包名称.class files
。
Any suggestions?
有什么建议?
EDIT: Here's the explanation, taken from the exercise description
编辑:这是来自练习描述的解释
sourcedir can be either absolute (starting with “/”) or relative to where we run the program from
sourcedir 可以是绝对的(以“/”开头)或相对于我们运行程序的位置
sourcedir is a given argument for the main method. how can I find that path?
sourcedir 是 main 方法的给定参数。我怎样才能找到那条路?
采纳答案by The Nail
Use this.getClass().getCanonicalName()
to get the full class name.
使用this.getClass().getCanonicalName()
来获得完整的类名。
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.
请注意,包/类名(“abC”)与 .class 文件的路径(a/b/C.class)不同,使用包名/类名来派生路径通常是不好的做法。类文件/包的集合可以在多个不同的类路径中,可以是目录或 jar 文件。
回答by Jon Egeland
There is a class, Class
, that can do this:
有一个类,Class
可以做到这一点:
Class c = Class.forName("MyClass"); // if you want to specify a class
Class c = this.getClass(); // if you want to use the current class
System.out.println("Package: "+c.getPackage()+"\nClass: "+c.getSimpleName()+"\nFull Identifier: "+c.getName());
If c
represented the class MyClass
in the package mypackage
, the above code would print:
如果c
表示MyClass
包中的类mypackage
,则上面的代码将打印:
Package: mypackage
Class: MyClass
Full Identifier: mypackage.MyClass
包:mypackage
类:MyClass
完整标识符:mypackage.MyClass
You can take this information and modify it for whatever you need, or go check the APIfor more information.
您可以获取此信息并根据需要对其进行修改,或者查看 API以获取更多信息。
回答by Bozho
The fully-qualified name is opbtained as follows:
完全限定名称的获取方式如下:
String fqn = YourClass.class.getName();
But you need to read a classpath resource. So use
但是您需要读取类路径资源。所以用
InputStream in = YourClass.getResourceAsStream("resource.txt");
回答by Zahangir Alam
use this.getClass().getName()
to get packageName.className
and use this.getClass().getSimpleName()
to get only class name
use this.getClass().getName()
to getpackageName.className
和 use this.getClass().getSimpleName()
to get only class name