Java-Uber jar(Maven Shaded)无法将资源视为URL

问题描述 投票:0回答:1

我有一个肥胖的超级瓶子(使用Maven Shade完成),它具有以下内容:

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

我尝试使用Paths.get(fileObject.getPath())并收到以下异常

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)

但是,当我尝试使用getResourceAsStream()并获得输入时,我可以读取文件!那么URL有什么问题呢?它与访问资源的特权有关吗?

java url inputstream maven-shade-plugin uberjar
1个回答
0
投票

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

我认为摘要是

1)CLassloader无法执行特权操作,例如在jar上下文之外访问文件信息。因此,将永远无法通过公开绝对路径的URL进行访问。

2)使用相对路径,资源对于jar总是“相对”的。因此,该项目是可见的。

© www.soinside.com 2019 - 2024. All rights reserved.