相关疑难解决方法(0)

运行jar时找不到类路径资源

在Spring Boot 1.1.5和1.1.6中都有这个问题 - 我正在使用@Value注释加载一个类路径资源,当我在STS(3.6.0,Windows)中运行应用程序时,它可以正常工作.但是,当我运行mvn包然后尝试运行jar时,我得到FileNotFound异常.

资源message.txt位于src/main/resources中.我检查了jar并验证它在顶层包含文件"message.txt"(与application.properties相同的级别).

这是应用程序:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application implements CommandLineRunner {

    private static final Logger logger = Logger.getLogger(Application.class);

    @Value("${message.file}")
    private Resource messageResource;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        // both of these work when running as Spring boot app from STS, but
        // fail after mvn package, and then running as java -jar
        testResource(new ClassPathResource("message.txt"));
        testResource(this.messageResource);
    }

    private void testResource(Resource resource) {
        try { …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

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

如何从资源文件夹中获取文件.Spring框架

我正在尝试解组我的xml文件:

public Object convertFromXMLToObject(String xmlfile) throws IOException {
    FileInputStream is = null;
    File file = new File(String.valueOf(this.getClass().getResource("xmlToParse/companies.xml")));
    try {
        is = new FileInputStream(file);
        return getUnmarshaller().unmarshal(new StreamSource(is));
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:java.io.FileNotFoundException:null(没有这样的文件或目录)

这是我的结构:

在此输入图像描述

为什么我无法从资源文件夹中获取文件?谢谢.

更新.

重构后,

URL url = this.getClass().getResource("/ xmlToParse/companies.xml"); 文件文件=新文件(url.getPath());

我可以更清楚地看到错误:

java.io.FileNotFoundException:/content/ROOT.war/WEB-INF/classes/xmlToParse/companies.xml(没有这样的文件或目录)

它试图找到WEB-INF/classes /我在那里添加了文件夹,但仍然收到此错误:(

在此输入图像描述

java spring-mvc fileinputstream java-io

19
推荐指数
3
解决办法
6万
查看次数

Spring Boot 可以找到资源文件,但 Java 类失败并出现 NullPointerException

我正在一个项目中,我需要在一个类中加载json文件src/main/resources

src/ 
   main/java/FirebaseTokenVerifier
   main/resources/staging_firebase.json
Run Code Online (Sandbox Code Playgroud)

该类在构造函数中ForebaseTokenVerifier将此JSON文件加载为

@Component
public class FirebaseTokenVerifier implements TokenVerifier {

  private FirebaseAuth firebaseAuth;

  // TODO (hhimanshu): resourceFile and DatabaseURL are hardcoded, this should be picked by external file
  // or should be picked up conditionally based on environment
  public FirebaseTokenVerifier() throws IOException, URISyntaxException {
    final String resourceName = "staging_firebase.json";
    System.out.println("Getting Resource: " + resourceName);
    Path path = Paths.get(ClassLoader.getSystemClassLoader().getResource(resourceName).toURI());

    FileInputStream serviceAccount = new FileInputStream(path.toFile());
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
        .setDatabaseUrl("https://staging-myapp.firebaseio.com") …
Run Code Online (Sandbox Code Playgroud)

java json java-8 spring-boot

5
推荐指数
1
解决办法
1050
查看次数

标签 统计

java ×3

spring-boot ×2

fileinputstream ×1

java-8 ×1

java-io ×1

json ×1

spring-mvc ×1