Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a file from resource folder

Tags:

java

jaxb

i have a maven project structure where i have src/main/resources/json/test.xml file present i am trying to read this using the following code but not able to read it.I ma getting cannot find the file specified.I have to pass a file object to unmarshal function ,how can i do this using other apporach

File file = new File("json\\test.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(ServiceApi.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            ServiceApi customer = (ServiceApi) jaxbUnmarshaller.unmarshal(file);
            System.out.println(customer.getService().size());

The exception is

javax.xml.bind.UnmarshalException
 - with linked exception:
[java.io.FileNotFoundException: C:\Users\jayesh_shah\Downloads\dbt-dataformstub\json\test.xml (The system cannot find the path specified)]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
    at com.ge.stub.api.jaxb.JAXBExample.main(JAXBExample.java:17)
Caused by: java.io.FileNotFoundException: C:\Users\jayesh_shah\Downloads\dbt-dataformstub\json\test.xml (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileInputStream.<init>(FileInputStream.java:79)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
    ... 6 more
like image 267
sheetal_r oswal Avatar asked Feb 18 '12 18:02

sheetal_r oswal


1 Answers

The resources folder is merged into the generated classes folder by maven when building. So you can get an InputStream for that file via:

InputStream is = YourClassName.class.getResourceAsStream("/json/test.xml");

JAXB can unmarshal from InputStreams, and the file is located using the same rules as used by the ClassLoader which defined the class YourClassName. (See getResourceAsStream for more info.)

like image 147
Hauke Ingmar Schmidt Avatar answered Nov 14 '22 05:11

Hauke Ingmar Schmidt