In Python you can get the path of the file that is executing via __file__
is there a java equivalent?
Also is there a way to get the current package you are in similar to __name__
?
And Lastly, What is a good resource for java introspection?
In Python you can get the path of the file that is executing via __file__
is there a java equivalent?
Also is there a way to get the current package you are in similar to __name__
?
And Lastly, What is a good resource for java introspection?
this.getClass() = current class
this.getClass().getPackage() = current package
Class.getName() = string of class name
Package.getName() = string of package name
I believe you're looking for the Reflection API to get the equivalent of introspection (http://download.oracle.com/javase/tutorial/reflect/).
@Christopher's answer addresses the issue of the class name.
AFAIK, the standard Java class library provides no direct way to get hold of the filename for an object's class.
If the class was compiled with the appropriate "-g" option setting, you can potentially get the classes filename indirectly as follows:
Throwable.getStackTrace()
.StackTraceElement.getFilename()
to fetch the source filename.Note this is potentially expensive, and there is no guarantee that a filename will be returned, or that it will be what you expect it to be.
You can get the folder (excluding packages) containing the class file:
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();