如何编写指向 JAR 文件外部的路径

How to write a path that points outside a JAR file

我在开发使用 Maven 编译的独立 Web 应用程序时遇到问题。

我在程序内部使用第三方软件(Tom Sawyer)需要指向JAR文件之外的文件。此外,我只能通过用户界面为其提供文件位置(即 C:path/to/file.json/../../file.json)。

但是,无论我输入什么软件,我都会收到错误 FileNotFound :

java.io.FileNotFoundException: JAR entry project/..\..\..\..\modelData.json not found in C:\Users\victor\Desktop\ea-prototype\Workspace_WEB\CSM\webappCSM_v0.1\target\Model-1.0.6-SNAPSHOT.jar!/BOOT-INF/classes
        at org.springframework.boot.loader.jar.JarURLConnection.throwFileNotFound(JarURLConnection.java:178) ~[Model-1.0.6-SNAPSHOT.jar:1.0.6-SNAPSHOT]
        at org.springframework.boot.loader.jar.JarURLConnection.connect(JarURLConnection.java:101) ~[Model-1.0.6-SNAPSHOT.jar:1.0.6-SNAPSHOT]
        at org.springframework.boot.loader.jar.JarURLConnection.getInputStream(JarURLConnection.java:165) ~[Model-1.0.6-SNAPSHOT.jar:1.0.6-SNAPSHOT]
        at java.base/java.net.URL.openStream(URL.java:1152) ~[na:na]

我想知道 JarURLConnection 是否能够处理来自 Jar 外部的文件,如果是,/../. 是否允许软件 'climb out'罐子?

谢谢

我找到了解决问题的方法。它非常特定于我正在使用的相关软件。不管怎样,它就在这里:

我选中了选项 'URL' 而不是 'File Path' 并写了一个文件 URL 位置,即

file:///C:/path/to/my/file.json

不过,它只适用于绝对路径,不适用于相对路径。