Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: find project name from within class

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?

like image 332
user1467855 Avatar asked Aug 16 '26 09:08

user1467855


1 Answers

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());
like image 120
FThompson Avatar answered Aug 18 '26 23:08

FThompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!