Is there a way to force a classloader to load a package even if none of its classes have been loaded?

后端 未结 3 1376
终归单人心
终归单人心 2021-01-04 02:54

Let\'s say a java codebase has a package called \"com.example\".

At runtime, we can get this Package by calling

Package p = Package.getPackage( \"com         


        
3条回答
  •  臣服心动
    2021-01-04 03:38

    I'm afraid that your assumptions are not valid. Classloaders do package book-keeping as they load classes.

    You can pass a wildcard to ClassLoader.getResources and force it to pick up the classes in a package, which will in turn do the work.

    You can make your own ClassLoader that calls definePackage, but that won't help you with the usual vanilla classloaders in use.

提交回复
热议问题