【发布时间】:2019-12-23 03:52:20
【问题描述】:
我有一个胖 uber jar(使用 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 有什么问题呢?是否与访问资源的权限有关?
【问题讨论】:
-
向我们展示您的代码。
-
Path为您提供文件系统上的路径。如果您想在 jar 中使用文件,请使用 InputStream 或getResourceAsStream()。 -
@michalk 正是我的想法:(。感谢您指出这一点。
标签: java url inputstream maven-shade-plugin uberjar