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

后端 未结 3 1375
终归单人心
终归单人心 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:30

    Alternatively you could use the class root directory as a starting point and walk through all *.class files and sub directories. This would only work if you know where all your .class files will reside beforehand.

    The cause of all this is that Java has dynamic classloading, so classes can be loaded at runtime from locations not known at compile time or even at startup time. Therefore the concept of a package is just a namespace for loaded classes, not a directory which you can use to look them up.

提交回复
热议问题