Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get all classes present in a package?

 
sneha ulhe
Ranch Hand
Posts: 52
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we get list of all classes present in any package,is there any method for that?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not possible in the general case, but for certain limited cases you can do it. Get the current ClassLoader, and if it's a URLClassLoader, then get its URLs, turn them into URIs, turn those into Files (if they are file URIs), and then list the files in the File object corresponding to the directory representing your package.

The question though is why? What are you trying to accomplish that you think you need to do this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic