Reputation: 18266
I would like to test if a particular package is known to the current classloader or system. Package.getPackage(String name) seems to return null thus im stuck. ClassLoader does not contain any method that does the same thus im stuck...
I d rather not load a class in the package to test if it is present, surely theres a better way.
Any pointers would be appreciated.
Upvotes: 3
Views: 279
Reputation: 10949
There's no easy way to get information on if a package exist, unless a class has been loaded from that package. Package.getPackage
does only work if a class from the named package has been loaded.
Your options are:
The 2nd option can fail if the code also is using url classloaders.
Upvotes: 1
Reputation: 8446
Package.getPackage(String name)
is the right tool for the job. Make sure you've
Package.getPackages()
to see what's availablegetClass().getPackage()
and then Package.getPackage(String name)
The method searches for the package in the current classloader only (or System classloader if current classloader is null). If no class in the package is loaded in the current classloader the package will no be present.
Upvotes: 4