相关疑难解决方法(0)

弹簧内部看不到jar中的文件

所有

我在里面创建了一个包含以下MANIFEST.MF的jar文件:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Main-Class: my.Main
Class-Path: . lib/spring-core-3.2.0.M2.jar lib/spring-beans-3.2.0.M2.jar
Run Code Online (Sandbox Code Playgroud)

在它的根目录中有一个名为my.config的文件,它在我的spring-context.xml中引用,如下所示:

<bean id="..." class="...">
    <property name="resource" value="classpath:my.config" />
</bean>
Run Code Online (Sandbox Code Playgroud)

如果我运行jar,除了加载特定文件外,一切看起来都很好:

Caused by: java.io.FileNotFoundException: class path resource [my.config] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/my.jar!/my.config
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:32)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:1)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 22 more
Run Code Online (Sandbox Code Playgroud)
  • 类从jar内部加载
  • spring和其他依赖项从分隔的jar中加载
  • 加载了spring上下文(新的ClassPathXmlApplicationContext("spring-context/applicationContext.xml"))
  • my.properties加载到PropertyPlaceholderConfigurer("classpath:my.properties")
  • 如果我将.config文件放在文件系统之外,并将资源url更改为'file:',一切似乎都很好......

有小费吗?

spring jar classpath

82
推荐指数
4
解决办法
7万
查看次数

从Spring Boot中的resources文件夹中读取文件

我正在使用Spring Boot和json-schema-validator.我正在尝试读取jsonschema.json从该resources文件夹调用的文件.我尝试了几种不同的方法,但我无法让它发挥作用.这是我的代码.

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("jsonschema.json").getFile());
JsonNode mySchema = JsonLoader.fromFile(file);
Run Code Online (Sandbox Code Playgroud)

这是文件的位置.

在此输入图像描述

在这里,我可以在文件classes夹中看到该文件.

在此输入图像描述

但是当我运行代码时,我得到以下错误.

jsonSchemaValidator error: java.io.FileNotFoundException: /home/user/Dev/Java/Java%20Programs/SystemRoutines/target/classes/jsonschema.json (No such file or directory)
Run Code Online (Sandbox Code Playgroud)

我在代码中做错了什么?

java spring spring-boot json-schema-validator

45
推荐指数
11
解决办法
11万
查看次数

java.io.FileNotFoundException:无法打开类路径资源,因为它不存在

我正在尝试为我的项目设置配置位置,但我不断收到以下错误:

java.io.FileNotFoundException:无法打开类路径资源[main/resources/app-context.xml],因为它不存在

我的项目设置如下:

在此输入图像描述

我将我的代码设置为:

ApplicationContext context = new ClassPathXmlApplicationContext(configLocation: "main/resources/app-context.xml");
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

java spring file intellij-14

12
推荐指数
2
解决办法
8万
查看次数

Spring Boot - 使用ResourceLoader读取文本文件

我正在尝试使用Spring资源加载器读取文本文件,如下所示:

Resource resource  = resourceLoader.getResource("classpath:\\static\\Sample.txt");
Run Code Online (Sandbox Code Playgroud)

该文件位于我的Spring启动项目中: 在此输入图像描述

它在eclipse中运行应用程序时工作正常,但是当我打包应用程序然后使用java -jar运行它时,我得到文件未找到异常:

java.io.FileNotFoundException: class path resource [static/Sample.txt] cannot be resolved to absolute file path because it does not reside in the
 file system: jar:file:/C:/workspace-test/XXX/target/XXX-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/Sample.txt
Run Code Online (Sandbox Code Playgroud)

我解压缩了Sample所在的Jar文件:XXX-0.0.1-SNAPSHOT\BOOT-INF\classes\static\Sample.txt

有谁可以帮助我吗 ?

提前致谢!

java spring spring-boot

11
推荐指数
2
解决办法
6万
查看次数