Java does not stop you from creating your own class my.company.Integer and my.other.company.Integer , so how can it not know which Integer class is correct.
Finishes work with the answer that I can offer is to create a predefined list of packages in which you want to search for the class, and continue each time until you find your class.
So something like:
class ClassFinder{ public static final String[] searchPackages = { "java.lang", "java.util", "my.company", "my.company.other" }; public Class<?> findClassByName(String name) { for(int i=0; i<searchPackages.length; i++){ try{ return Class.forName(searchPackages[i] + "." + name); } catch (ClassNotFoundException e){
If you want to get a list of all available packages instead of hard coding, see here .
It should be warned that this method is unlikely to work very well, so use it sparingly.
source share