java src/main/resources 中的文件不在类路径中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34872496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file in src/main/resources is not in classpath
提问by wazz
I created a simple program to check if spring-context define a file in src/main/resources folder.
我创建了一个简单的程序来检查 spring-context 是否在 src/main/resources 文件夹中定义了一个文件。
I have this file structure:
我有这个文件结构:
project
--> src/main/resources/spring-config.xml
--> src/main/resources/testfile02
and I try to access to these files using this test class
我尝试使用这个测试类访问这些文件
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();
}
}
}
}
And I can't understand why I get FileNotFound exception, during classpathResource.getInputStream() if the ClassPathXmlAppContext is working ok.
如果 ClassPathXmlAppContext 工作正常,我不明白为什么在 classpathResource.getInputStream() 期间会出现 FileNotFound 异常。
execution log:
执行日志:
янв 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
The project is built using gradle:
该项目是使用 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:
.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>
I tried to detect systemproperty java.class.path, and I've got {project}/bin and jars for spring there and that's all. No resources folder. How can I get access to resources from src/main/resources as I get access to src/main/resources/spring-config.xml?
我试图检测系统属性 java.class.path,并且我有 {project}/bin 和用于 spring 的 jars,仅此而已。没有资源文件夹。当我访问 src/main/resources/spring-config.xml 时,如何从 src/main/resources 访问资源?
回答by Wim Deblauwe
You don't need to add the 'classpath:' prefix when using the constructor. This works:
使用构造函数时,您不需要添加 '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();
}
}
}
}
回答by Essex Boy
The problem is the vanilla Java which is how you're running it will not add src/main/resources to the classpath, that is a convention that other runners use.
问题是 vanilla Java,它是您运行的方式,它不会将 src/main/resources 添加到类路径,这是其他跑步者使用的约定。
Below works fine.
下面工作正常。
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();
}
}
}
}