简体   繁体   English

无法从 spring boot .jar 读取文件

[英]Unable to read file from spring boot .jar

I want to access file on my classpath called reports/invoiceSweetChoice.jasper in jar on production server.我想访问生产服务器上 jar 中名为reports/invoiceSweetChoice.jasper类路径上的文件。 Whatever I do I get null.无论我做什么,我都会得到空值。

I have tried this.getClass().getResourceAsStream("reports/invoiceSweetChoice.jasper") , tried via InputStream etc. I have printed out content of System.getProperty("java.class.path") and it is empty.我已经尝试过this.getClass().getResourceAsStream("reports/invoiceSweetChoice.jasper") ,通过InputStream等尝试过。我已经打印出System.getProperty("java.class.path") ,它是空的。 Not sure how is that possible.不知道这怎么可能。 Do you have any suggestion how to resolve this ?你有什么建议如何解决这个问题吗?

@Autowired private ResourceLoader resLoad;


void someMethod() {
    Resource r = resLoad.getResource("classpath:reports/file.abc");
    r.getInputStream()...
    ...

In manifest.mf, the classpath is defined using the class-path key and a space-delimited list of files , as follows:在 manifest.mf 中,类路径是使用class-path 键和空格分隔的文件列表定义的,如下所示:

MANIFEST.MF at root of jarfile. jarfile 根目录下的 MANIFEST.MF。

Class-Path: hd1.jar path/to/label007.jar path/to/foo.jar

If there are spaces in the jar filename, you should enclose them in quotes.如果 jar 文件名中有空格,则应将它们括在引号中。

If it's a webapp, the reports path should be in your BOOT-INF subdirectory of your classpath -- this is automatically performed by maven if you put it in src/main/resources in the standard layout.如果它是一个 web 应用程序,报告路径应该在你的类路径的 BOOT-INF 子目录中——如果你把它放在标准布局的 src/main/resources 中,这将由 maven 自动执行。

EDIT:编辑:

Now that you've clarified what you're trying to do, you have 2 approaches.现在您已经阐明了您要做什么,您有两种方法。 Like I said above, you can grab the file from the BOOT-INF subdirectory of your webapp or you can enumerate the entries in the jar until you find the one you want:就像我上面说的,您可以从 web 应用程序的 BOOT-INF 子目录中获取文件,或者您可以枚举 jar 中的条目,直到找到您想要的条目:

JarInputStream is = new JarInputStream(new FileInputStream("your/jar/file.jar"));
JarEntry obj = null
while ((obj = is.getNextJarEntry()) != null) {
   JarEntry entry = (JarEntry)obj;
   if (entry.getName().equals("file.abc")) {
        ByteArrayOutputStream baos = new ByetArrayOutputStream(); 
        IOUtils.copy(jarFile.getInputStream(entry), baos);
        String contents = new String(baos.toByteArray(), "utf-8");
        // your entry is now read into contents
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取spring boot包装jar文件 - Can not read spring boot packaging jar file Spring Boot无法从配置文件特定的yaml文件读取 - Spring boot unable to read from profile specific yaml file Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件 - Spring boot fat jar cannot read file from absolute path defined in application.properties Spring 在 docker 上启动无法从资源中读取属性文件,但是可以在没有 Docker 的情况下使用 Z93F725A4jar423D21C28 - Spring boot on docker not able to read a property file from resources, but the can without Docker with java -jar Spring 引导应用程序从 jar 的属性文件中读取,在运行测试时添加为依赖项 - Spring boot application to read from a properties file of a jar added as a dependency while running test 无法运行Spring Boot Maven应用程序的jar文件 - Unable to Run jar File of Spring Boot Maven Application 无法访问外部 xml 配置文件 spring 启动 jar - unable to access outside xml config file in spring boot jar Spring boot,无法读取文件,启动时使用jar包 - Spring boot, The file can not be read, Use jar package when starting Spring Boot:无法使用lib / *。jar中的类读取lib / *。jar中的资源 - Spring Boot: Unable to read resources in lib/*.jar using a class in lib/*.jar Spring 引导强制 PropertySource 从内部读取 jar - Spring boot force PropertySource to read from internal jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM