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

标签 java url inputstream maven-shade-plugin uberjar

我有一个 fat 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 有什么问题呢?是否与访问资源的权限有关?

最佳答案

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

我认为总结是

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

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

关于Java - Uber jar (Maven Shaded) 无法将资源视为 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531124/

相关文章:

Java从文本文件中的某个点提取文本

java - InputStream音频混合(MODE_STREAM)

java - JFormattedTextField 严格解析数字格式

java - 打开连接时出错 java.io.IOException : Server returned HTTP response code: 501 for URL

python - 使用 Python 从电子邮件中提取 URL

java - 在 OutputStream 中保存一个字节有什么区别

Java:干净地将异常重定向到字符串

java - 为什么要在 Web 服务中使用 complexType?

php - 使用 RewriteRule 时发生内部服务器错误

java - 从java应用程序下载文件