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?
Python doesn't have any exact equivalent to a . jar file.
__class__ is an attribute on the object that refers to the class from which the object was created. a. __class__ # Output: <class 'int'> b. __class__ # Output: <class 'float'> After simple data types, let's now understand the type function and __class__ attribute with the help of a user-defined class, Human .
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With