How to walk through Java class resources?

后端 未结 5 2000
栀梦
栀梦 2020-12-13 20:35

I know we can do something like this:

Class.class.getResourceAsStream(\"/com/youcompany/yourapp/module/someresource.conf\")

to read the fil

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 21:25

    The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)

    List resourceNames;
    try (ScanResult scanResult = new ClassGraph()
                .whitelistPaths("com/yourcompany/yourapp")
                .scan()) {
        resourceNames = scanResult.getAllResources().getNames();
    }
    

提交回复
热议问题