How can I use java reflection or similar to find project name by just having an instance of the class? If not the project name -- which is what I really want -- can I find the package name?
Projects are simply organizational tools used by IDEs, and therefore the project name is not information contained in the class or the JVM.
To get the package, use Class#getPackage(). Then, Package#getName() can be called to get the package as the String you see in your code's package declaration.
Class<?> clazz = Application.class;
Package package = clazz.getPackage();
System.out.println(package.getName());
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