0

I'm trying to get the relative path for a file but without success.The absolute path is:"/Users/dev/Documents/projects/testService/src/main/resources/response.xml". After i deploy my code I'm getting java.io.FileNotFoundException:(No such file or directory).

Can you please help me? Thank you

0

The code looks like this:

 try {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/main/resources/response.xml"));


        while(xsr.hasNext()) {
            xsr.next();
            if (xsr.getEventType() == XMLStreamConstants.START_DOCUMENT || xsr.getEventType() == XMLStreamConstants.END_DOCUMENT ||xsr.isCharacters() == true || xsr.hasText() == true || xsr.getEventType() == XMLEvent.END_ELEMENT)
                continue;
            QName name = xsr.getName();
            int index = name.toString().indexOf('}');
            String aa = name.toString().substring(index+1);

         if (xsr.isStartElement() && aa.contains("Bonitae"))
               break;
        }

        jaxbContext = JAXBContext.newInstance(Customer.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        response = unmarshaller.unmarshal(xsr, Customer.class);    
    }

On my local pc it's working fine. I deployed the project using a jar.

Alexandra
  • 21
  • 3
  • 2
    Could you paste the code you used as well, that would be helpful. – Timothy T. Mar 25 '19 at 13:43
  • please show us your code or what you've done so far – Onkar Musale Mar 25 '19 at 13:43
  • In the snippet you posted you are already using a relative path? – T A Mar 25 '19 at 13:54
  • /src/main/resources/response.xml - is it the same you get by clicking on file properties and path label – Onkar Musale Mar 25 '19 at 13:56
  • Where do you run your code from, the directory above `src`? –  Mar 25 '19 at 13:57
  • @OnkarMusale, if i click on copy path file i''m getting this: /Users/dev/Documents/projects/testService/src/main/resources/response.xml – Alexandra Mar 25 '19 at 14:02
  • /Users/dev/Documents/projects/testService/src/main/resources/response.xml - then add this path in your code – Onkar Musale Mar 25 '19 at 14:04
  • Doesn't working: ------------------------------------- java.io.FileNotFoundException: /Users/dev/Documents/projects/testServie/src/main/resources/response.xml (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) On my locat desktop it works. After I deployed the project doesn't worked anymore – Alexandra Mar 25 '19 at 14:12
  • An entry inside a .jar file is not a file. You cannot read it using FileReader, ever. Only [Class.getResourceAsStream](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Class.html#getResourceAsStream%28java.lang.String%29) will read it properly. – VGR Mar 25 '19 at 14:56

0 Answers0