简体   繁体   中英

Using file from within JAR, while runnig app from jar

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 Reading a resource file from within jar or How do I read a resource file from a Java jar file? ) Here is my target tree, and resources: 在此处输入图像描述 在此处输入图像描述

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);
  }

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM