使用JAR内部的文件,在JAR中运行应用程序时。

huangapple 未分类评论46阅读模式
英文:

Using file from within JAR, while runnig app from jar

问题

我试图在以JAR形式运行应用程序时使用文件。
当我通过Intelij运行应用程序时,一切正常。然而,当我尝试通过jar运行时,无法访问该文件。
我尝试阅读了一些包含类似问题的主题,但没有一个能帮助到我
(例如https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar 或 https://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file?rq=1
这是我的目标树和资源:使用JAR内部的文件,在JAR中运行应用程序时。
使用JAR内部的文件,在JAR中运行应用程序时。

当我使用

String path = String
        .join("", "classpath:static\\assets\\config\\", fileName);
File file = ResourceUtils.getFile(path); 
InputStream targetStream = new FileInputStream(file);

在Intelij运行期间,一切正常。

在jar的情况下,我尝试了:

String path = String
        .join("", "static/assets/config/", fileName).replace("\\","/")).toExternalForm();
String path2 = String
        .join("", "static/assets/config/", fileName).replace("\\","/")).getFile();
String path3 = String
        .join("", "static/assets/config/", fileName).replace("\\","/")).getPath();

以及其他许多方法。它们的结果是正确的路径,例如:

  • file:/D:/Projects/myProject/target/classes/static/assets/config/fileName(对于toExternalForm)
  • /D:/Projects/myProject/target/classes/static/assets/config/fileName(对于getFile)

然而,当我尝试:

 InputStream in = getClass().getResourceAsStream(everyPath);

时,所有这些方法都会导致空的InputStream,并且我会得到一个错误:
java.io.FileNotFoundException: D:\Projects\myProject\target\project-app-1.0.jar\BOOT-INF\classes\static\assets\config\fileName(系统找不到指定的路径)
当我通过7zip打开project-app-1.0.jar时,路径确切为:
D:\Projects\myProject\target\project-app-1.0.jar\BOOT-INF\classes\static\assets\config\fileName

这是我的资源处理程序的外观:

  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
      "classpath:/resources/", "classpath:/static/"};
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations(
        CLASSPATH_RESOURCE_LOCATIONS);
  }
英文:

I am trying to use file, when running application as JAR.
When I run application through Intelij, everything is fine. However when I try to run it via jar, I cannot access the file.
I tried to read few topics containing similar matter, but non of them help
(like https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar or https://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file?rq=1
)
Here is my target tree, and resources:使用JAR内部的文件,在JAR中运行应用程序时。
使用JAR内部的文件,在JAR中运行应用程序时。

When I use

String path = String
        .join("", "classpath:static\assets\config\", fileName);
File file = ResourceUtils.getFile(path); 
InputStream targetStream = new FileInputStream(file)

During intelij run, everything works.

In the case of jar, I tried:

String path = String
        .join("", "static\assets\config\", fileName).replace("\\","/")).toExternalForm();
String path2 = String
        .join("", "static\assets\config\", fileName).replace("\\","/")).getFile();
String path3 = String
        .join("", "static\assets\config\", fileName).replace("\\","/")).getPath();

and many other. They result in correct path, for example:

  • file:/D:/Projects/myProject/target/classes/static/assets/config/fileName (in case of toExternalForm)

  • /D:/Projects/myProject/target/classes/static/assets/config/fileName (in case of getFile)

    However all of them results in null InputStream, when I try:

 InputStream in = getClass().getResourceAsStream(everyPath);

I get an error:
java.io.FileNotFoundException: D:\Projects\myProject\target\project-app-1.0.jar\BOOT-INF\classes\static\assets\config\fileName (The system cannot find the path specified)
When the path in the project-app-1.0.jar when I open it by 7zip is exactly:
D:\Projects\myProject\target\project-app-1.0.jar\BOOT-INF\classes\static\assets\config\fileName

This is how my resource handler looks like:

  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
      "classpath:/resources/", "classpath:/static/"};
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations(
        CLASSPATH_RESOURCE_LOCATIONS);
  }

答案1

得分: 0

forget about "files" when you want to use something inside your jar, its just a simple "Resource" that your have to use with getResource.

If you use a standard packaging system, everything inside "resources" folder are put at the root of your JAR, so if you want to read your "foo.txt" file inside "static\assets\config" folder you have to do use method:

InputStream in = ClassLoader.getSystemResourceAsStream("static/assets/config/foo.txt");

英文:

forget about "files" when you want to use something inside your jar, its just a simple "Resource" that your have to use with getResource.

If you use a standard packaging system, everything inside "resources" folder are put at the root of your JAR, so if you want to read your "foo.txt" file inside "static\assets\config" folder you have to do use method:

InputStream in = ClassLoader.getSystemResourceAsStream("static/assets/config/foo.txt");

huangapple
  • 本文由 发表于 2020年4月3日 23:32:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61015318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定