【问题标题】:file in src/main/resources is not in classpathsrc/main/resources 中的文件不在类路径中
【发布时间】:2016-01-19 09:02:57
【问题描述】:

我创建了一个简单的程序来检查 spring-context 是否在 src/main/resources 文件夹中定义了一个文件。

我有这个文件结构:

project
--> src/main/resources/spring-config.xml
--> src/main/resources/testfile02

我尝试使用这个测试类访问这些文件

public class ClasspathTest {

    public static void main(String args[]) throws URISyntaxException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");

        ClassPathResource testfile02 = new ClassPathResource("classpath:testfile02");

        if (testfile02 != null) {
            try (InputStream inputStream = testfile02.getInputStream();
                Reader streamReader = new InputStreamReader(inputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(streamReader)) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }

    }

}

如果 ClassPathXmlAppContext 工作正常,我无法理解为什么在 classpathResource.getInputStream() 期间出现 FileNotFound 异常。

执行日志:

янв 19, 2016 11:57:48 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Tue Jan 19 11:57:48 MSK 2016]; root of context hierarchy
янв 19, 2016 11:57:48 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
java.io.FileNotFoundException: class path resource [classpath:testfile02] cannot be opened because it does not exist

项目是使用gradle构建的:

apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework:spring-context:4.1.6.RELEASE'
}

.classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="src" path="src/test/resources"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

我试图检测 systemproperty java.class.path,我有 {project}/bin 和 spring 罐子,仅此而已。没有资源文件夹。 当我可以访问 src/main/resources/spring-config.xml 时,如何从 src/main/resources 访问资源?

【问题讨论】:

  • testfile01 还是testfile02? (您是否检查了target/classes 或实际的 jar 内容?)
  • 对不起,我在问题上写错了,名称相同(testfile02),所以我的问题仍然存在
  • 如何运行测试?使用 maven 或 gradle 或 IDE ?您是否将src/main/resources 设置为源路径?
  • 您好 Wim,它是使用 gradle 构建的,我在问题中添加了构建文件。文件夹 src/main/resources 是一个源路径,您可以在 .classpath 中看到
  • 你需要在构造函数参数中有classpath:吗?你可以试试new ClassPathResource("testfile02") 吗?

标签: java spring


【解决方案1】:

使用构造函数时不需要添加'classpath:'前缀。这有效:

public class ClasspathTest {

    public static void main(String args[]) throws URISyntaxException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");

        ClassPathResource testfile02 = new ClassPathResource("testfile02");

        if (testfile02 != null) {
            try (InputStream inputStream = testfile02.getInputStream();
                Reader streamReader = new InputStreamReader(inputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(streamReader)) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }

    }

}

【讨论】:

    【解决方案2】:

    问题是 vanilla Java,它是您运行的方式,它不会将 src/main/resources 添加到类路径中,这是其他运行者使用的约定。

    下面的工作正常。

    package com.greg;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(value = { "classpath:spring-config.xml",
            "classpath:testfile02.xml" })
    public class ClasspathTest {
    
        @Test
        public void test1() {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    "classpath:spring-config.xml");
    
            ClassPathResource testfile02 = new ClassPathResource("testfile02.xml");
    
            if (testfile02 != null) {
                try {
                    InputStream inputStream = testfile02.getInputStream();
                    Reader streamReader = new InputStreamReader(inputStream,
                            "UTF-8");
                    BufferedReader bufferedReader = new BufferedReader(streamReader);
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException exc) {
                    exc.printStackTrace();
                }
            }
    
        }
    }
    

    【讨论】:

    • 与 JUnit 跑步者或纯 Java 无关。重要的一点是从ClassPathResource 的构造函数参数中删除“类路径:”。
    • 我认为您的问题定义是正确的。无论如何,这些文件在运行时位于 /bin/ 文件夹中,并且类路径有它的文件夹。正如我所见,ClassPathResource 不支持“classpath:”前缀作为 ClassPathXmlApplicationContext 做
    猜你喜欢
    • 1970-01-01
    • 2018-09-16
    • 2017-08-24
    • 2017-07-20
    • 1970-01-01
    • 2019-12-21
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多