web-dev-qa-db-fra.com

IOException analysant un document XML à partir d'une ressource de chemin de classe

Ok je suis en train d'essayer de minimiser un projet. Cependant, mon projet ne parvient pas à trouver le fichier XML contenant les beans. combiné2.xml

Je l'ai défini comme:

    public RepeatingGrpPoC() {
    appContext = new ClassPathXmlApplicationContext(
            new String[] { "src/main/Java/resources/combined2.xml",});
    c = 0;    
}

Cependant pour une raison inconnue de moi, je reçois constamment l'erreur.

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/Java/resources/combined2.xml]; nested exception is Java.io.FileNotFoundException: class path resource [src/main/Java/resources/combined2.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.Java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.Java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.Java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.Java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.Java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.Java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.Java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.Java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.Java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.Java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:395)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.Java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.Java:93)
at metadataPoC.RepeatingGrpPoC.<init>(RepeatingGrpPoC.Java:34)
at metadataPoC.Main.main(Main.Java:22)

Causée par: Java.io.FileNotFoundException: la ressource de chemin de classe [src/main/Java/resources/combine2.xml] ne peut pas être ouverte car elle n'existe pas à org.springframework.core.io.ClassPathResource.getInputStream (ClassPathResource.Java:141) sur org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions (XmlBeanDefinitionReader.Java:328) ... 14 plus

Où le programme chercherait-il ce fichier depuis que je lui ai donné le chemin relatif?

11
Will

Il essaie de charger ce fichier à partir du classpath et ne le trouve pas. Essayez de spécifier simplement "combined2.xml" au lieu de "src/main/Java/resources/combined2.xml" et assurez-vous que src/main/Java/resources se trouve sur votre chemin de classe.

En passant, dans Maven, le répertoire standard des ressources est src/main/resources. Je vous suggère donc de placer ce fichier là.

31
dogbane

Maven a un répertoire standard pour les ressources qui est src/main/resources, donc si vous gardez votre fichier ici, il le prendra ..__ et dans le chemin, donnez simplement le nom du fichier.

Par exemple

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");

J'ai eu le même problème cela a fonctionné pour moi

1
Varun

Essaye ça

appContext = new ClassPathXmlApplicationContext(
            new String[] { "/**/combined2.xml", "/**/xxx.xml"});
0
Anand Devaraj
You can use the relative path of the xml file.
relative path: path relative to your package where the XML file is located.

E.g. 
Assume,
package = beanfactory,  
xml file name = application-context.xml, 
and xml file in under this package.
then provide the path as "/beanfactory/application-context.xml"
ApplicationContext factory=new 
ClassPathXmlApplicationContext("/beanfactory/application-context.xml");
This works without errors.
0
Vinod Kumar