Spring Boot: what would be the reasons for a resource file not to be reachable if running from a jar?

huangapple go评论80阅读模式
英文:

Spring Boot: what would be the reasons for a resource file not to be reachable if running from a jar?

问题

我正在开发一个Spring Boot应用程序。我需要加载一个位于资源中的文件。如果我运行单元测试(使用Maven,以防万一)或者甚至运行集成测试,文件可以被成功加载。但是,当我从打包的JAR文件中运行应用程序时,实际上,它就找不到文件了。我尝试了几种不同的方法来访问它,但它们都失败了:

java.lang.IllegalArgumentException: resource path-to-file not found.
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217) ~[grpc-1.23.jar!/:?]
        at com.google.common.io.Resources.getResource(Resources.java:195) ~[grpc-1.23.jar!/:?]

另一种方式:

java.io.FileNotFoundException: class path resource [path-to-file] cannot be resolved to absolute file path because it does not exist
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:177) ~[spring-core-5.1.7.RELEASE.jar!/:5.1.7.RELEASE]

文件位于 WEB-INFO/classes/path-to-file,这来自于我的项目中的 src/main/resources/path-to-file

感谢任何超出传统思维的建议。

更新 1
这让它在JAR文件中运行,并且代码足够简单:

ClassPathResource cl = new ClassPathResource(resourcePath);
URL url = cl.getURL();
String content;
try (InputStream in = url.openStream()) {
  content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
LOG.info("Content from {} is {} bytes long", resourcePath, content.length());
return content;

这个提示来自于被标记为已接受答案的回答。谢谢!

英文:

I am working on a springboot application. I need to load a file that is in resources. The file can be loaded without issues if I run unit tests (maven, just in case)... or even an integration test. But when I run the application say, for real, from the packaged jar, then it can't be found. I have tried with a couple of different ways to reach it but they fail:

java.lang.IllegalArgumentException: resource path-to-file not found.
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:217) ~[grpc-1.23.jar!/:?]
        at com.google.common.io.Resources.getResource(Resources.java:195) ~[grpc-1.23.jar!/:?]

Another way:

java.io.FileNotFoundException: class path resource [path-to-file] cannot be resolved to absolute file path because it does not exist
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:177) ~[spring-core-5.1.7.RELEASE.jar!/:5.1.7.RELEASE]

The file is in WEB-INFO/classes/path-to-file which is coming from src/main/resources/path-to-file in my project.

Thanks for any outside-of-the-box ideas.

Update 1
This made it work from jar, and the code is simple enough:

    ClassPathResource cl = new ClassPathResource(resourcePath);
    URL url = cl.getURL();
    String content;
    try (InputStream in = url.openStream()) {
      content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
    }
    LOG.info("Content from {} is {} bytes long", resourcePath, content.length());
    return content;

The tip came from the answer that is marked as accepted. Thanks!

答案1

得分: 1

也许这会回答你的问题:
https://stackoverflow.com/questions/44411680/cannot-read-file-within-jar-file

根据我的经验,在处理JAR文件中的资源时,我会使用InputStream而不是其他方法。

英文:

May be this would answer your question:
https://stackoverflow.com/questions/44411680/cannot-read-file-within-jar-file

From my experience, to deal with resource inside jar, i would using InputStream instead

huangapple
  • 本文由 发表于 2020年8月7日 22:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63303891.html
匿名

发表评论

匿名网友

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

确定