0

I am coding on Eclipse. Under a package named com.something.drivers I've created a number of subpackages containing classes that implement some common interfaces. This is an example of the Driver pattern, so classes under a subpackage will be loaded dynamically from the main application. As all classes in every driver subpackage have the same name, the application can guess the qualified name of a class just after the package name.

Now I am trying to get the list of the installed drivers (subpackages under com.something.driver package). I don't want to maintain a manually edited list but just get it from the actually installed drivers.

I read List all subpackages of a package and learn how to use Package class, but I've founded that Package.getPackage(com.something.driver).getPackages() returns null because the classes in the subpackages under com.something.driver are only loaded dynamically, and one at a time.

I'm lost at this point. I suppose Package class is not the way but probably navigate the class tree checking with getResource?

Thaks in advance.

coterobarros
  • 941
  • 1
  • 16
  • 25

1 Answers1

1

Almost certainly the best way to deal with this is to create a list as a build step (probably as a .java file).

Package.getPackage is deprecated.

Package.getPackages is a static method - it isn't doing what you think it's doing.

Both method use the caller's class loader, which is surprising, and isn't much use if you're "really" dynamically loading the classes.

For unloaded classes, I guess you're looking at getting hold of the class path for your class loader (assuming it's a URLClassLoader of some sort) and traversing directories/read jar/zip directories (or the manifest if present).

Modules may be useful - I've not really looked at them.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Thank you for your answer @Tom. Your idea of traversing directories from a local class path is a good idea. I will test it inside Eclipse and after exporting the application as a jar. – coterobarros Jan 14 '19 at 09:34
  • Modules sounds good and probably the perfect solution, but I am compiling for Java 8 and don't want to spend much time learning something new just now. – coterobarros Jan 14 '19 at 09:34