I created a simple program to check if spring-context define a file in src/main/resources folder.
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.
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:
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>
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?
You don't need to add the 'classpath:' prefix when using the constructor. This works:
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();
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With