score:185

Accepted answer

use this.getclass().getcanonicalname() to get the full class name.

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.

score:4

use this.getclass().getname() to get packagename.classname and use this.getclass().getsimplename() to get only class name

score:14

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");

score:32

there is a class, class, that can do this:

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:

package: mypackage
class: myclass
full identifier: mypackage.myclass

you can take this information and modify it for whatever you need, or go check the api for more information.


Related Query

More Query from same tag