简体   繁体   中英

Java - Uber jar (Maven Shaded) cannot see resource as a URL

I have a fat uber jar (done using Maven Shade), which has the following contents:

FATTY-UBER-FATTY
\- mainAppPackage
   +- some.class.file
\- otherdep
   +- some.other.dep.class
someprop.properties
logging.properties
\- META-INF
   - MANIFEST.MF

I tried using Paths.get(fileObject.getPath()) and got the following exception

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\projectdir\target\projectdir-0.0.1-SNAPSHOT-shaded.jar!\logging.properties
        at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
        at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
        at java.nio.file.Paths.get(Paths.java:84)
        at mainAppPackage.main.app.MainApp.main(MainApp.java:32)

HOwever, when I try to use getResourceAsStream() and get the input, i can read the file! So what's the issue with the URL then? Is it something related to privileges for accessing resources ?

https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html

I think the summary is

1) CLassloader cannot do privileged action such as access file info outside the context of the jar. Hence, the access will never be possible through an URL that exposes absolute path.

2) Using relative path, the resource will always be "Relative" to the jar. Hence, the item is visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM