classgraph / classgraph

An uber-fast parallelized Java classpath scanner and module scanner.
MIT License
2.73k stars 285 forks source link

Consider bridge to nio Path #868

Open oliviercailloux opened 2 months ago

oliviercailloux commented 2 months ago

I’d love to be able to get a Path from the result of a scan. This would permit to use code that accepts Path instances as input, and thus to write code able to exploit hierarchies of elements coming from both “normal” file systems and classpaths or module paths. Is there any obstacle to do so?

You might want to consider providing, for example, Resource#toNioPath, or similar.

lukehutch commented 2 months ago

Hi, I believe you can just get the URI of a resource, then you can directly get the Path from that. Does that work for you? Or do you believe that there should also be a separate getPath?

On Sun, Jun 16, 2024, 9:24 AM Olivier Cailloux @.***> wrote:

I’d love to be able to get a Path https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html from the result of a scan. This would permit to use code that accepts Path instances as input, and thus to write code able to exploit hierarchies of elements coming from both “normal” file systems and classpaths or module paths. Is there any obstacle to do so?

Otherwise, please consider providing, for example, Resource#toNioPath https://www.javadoc.io/doc/io.github.classgraph/classgraph/latest/io/github/classgraph/Resource.html, or similar.

— Reply to this email directly, view it on GitHub https://github.com/classgraph/classgraph/issues/868, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGGCKM4E5OH3WFRJIPGMGLZHWU4RAVCNFSM6AAAAABJMV3YCKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGM2TKOBTG42TEMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

oliviercailloux commented 2 months ago

I just tried that. As I expected, the URI returned by the resource is the same as the URL returned by Class#getResource, namely, something like jar:file:/….jar!/…, and trying to turn this into a Path yields a FileSystemNotFoundException.

StackOverflow has proposed workarounds to deal with this but the reason I am trying to use Classgraph is precisely to avoid having to deal with such difficulties.

lukehutch commented 2 months ago

If there is any ! delimiter in a URL (and/or the URL starts with jar:), then the only way to read that resource is by opening it as a URL. Neither Path nor URI can read files from inside a jarfile.

However, you can directly use ClassGraph's Resource API to open the resources you need to read.

For other resource URLs starting with file:/, you should be able to get the Path directly (I don't think you need to strip the file:/ prefix, but I don't remember).

oliviercailloux commented 2 months ago

If there is any ! delimiter in a URL (and/or the URL starts with jar:), then the only way to read that resource is by opening it as a URL. Neither Path nor URI can read files from inside a jarfile.

It is actually possible to read such an URL using a Path, by first creating the related file system, as indicated on the link to SO that I provided here above. This request is in hope that Classgraph would do this for its users.

lukehutch commented 2 months ago

It's complicated, because you can have jars nested inside jars nested inside jars... just splitting once at ! won't solve the problem.

Does this work for you?

https://stackoverflow.com/a/48298758/3950982

oliviercailloux commented 2 months ago

I agree that it is complicated (that is a further motive for my request). But feasible.

The approach suggested here seems sensible to me (try to get a path, create FS if required). I have not tried that under all conditions of course (this request is precisely in hope that classgraph would solve that problem for me), but certainly something like this should work, even in case of jars within jars.

If you can provide something that works for the simple case of a non-embedded jar, it will be good enough for my specific use case, but of course I suppose that supporting (perhaps on the longer term) a more general solution would be most valuable to your users.

lukehutch commented 2 months ago

Can you please verify this works in all cases you care about, and put together a PR that handles these cases (including the issues in the answer you linked affecting JDK 9-12)? See also the last link I shared.