0

I have a simple java plugin project which needs read an XML file. I am using java.io File to read my file. The program works perfectly when I give the file path as D:\Studies\HospitalManagement [AbulanceService]\config\AmbulanceService.xml. But I have been asked to give the path as config/AmbulanceDetails.xml. Since I am a newbie to this file reading concept, I can't understand how to make this work. When I try to pass the file path as mentioned in the second bolded text, it gives me an exception. I tried using getAbsolutePath() method and putting the XML file in another source folder which was suggested in several similar questions in StackOverflow but did not resolve my issue. How can I solve this issue? A massive thanks for helping me!

The source code :-

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class AmbulanceServiceImpl implements AmbulanceService {

    private static final String AMBULANCE = "Ambulance";
    private static final String AVAILABILITY = "Availability";
    private static final String AVAILABLE = "Available";
    private static final String REGISTRATION_NO = "RegNo";
    private static final String DRIVERNAME = "Driver";
    private static final String NO_VEHICLE_AVALABLE_MESSAGE = "Sorry! No ambulance available currently";
    private static final String PATH = "config/AmbulanceDetails.xml"; //this is the way I want to specify my file path


    private NodeList nodeList;

    private void readXml() {

        File file = new File(PATH); //passing the file path 
        try {


            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(file);
            document.getDocumentElement().normalize();

            nodeList = document.getElementsByTagName(AMBULANCE);


        } catch (ParserConfigurationException | SAXException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String findAvailableAmbulance() {

       //some other process

    }



    @Override
    public String getAnAmbulance() {
        // TODO Auto-generated method stub
        readXml();
        return findAvailableAmbulance();
    }

}

The exception :-

java.io.FileNotFoundException: D:\Softwares\eclipse\config\AmbulanceDetails.xml (The system cannot find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
    at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
    at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:652)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:150)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
    at hospitalmanagement__abulanceservice_.AmbulanceServiceImpl.readXml(AmbulanceServiceImpl.java:37)
    at hospitalmanagement__abulanceservice_.AmbulanceServiceImpl.getAnAmbulance(AmbulanceServiceImpl.java:80)
    at hospitalmanagement__abulanceservice_.Activator.start(Activator.java:12)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:842)
    at org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:834)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:791)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:1015)
    at org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:365)
    at org.eclipse.osgi.container.Module.doStart(Module.java:603)
    at org.eclipse.osgi.container.Module.start(Module.java:467)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel$2.run(ModuleContainer.java:1844)
    at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor$1$1.execute(EquinoxContainerAdaptor.java:136)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1837)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1780)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1742)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1664)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:234)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:345)

Project Folder structure :-

Project folder structure

GPI
  • 9,088
  • 2
  • 31
  • 38
  • you should set System property "user.dir" to the directory that your relative file names should be relative to. – ControlAltDel Mar 09 '20 at 15:46
  • Can you please tell me how to that with an example??? @ControlAltDel – Manujaya Premathilaka Mar 09 '20 at 15:47
  • 1
    `System.setProperty("user.dir","/usr/var/");` – ControlAltDel Mar 09 '20 at 15:49
  • And exactly what should pass for the second parameter?? Thanks! @ControlAltDel – Manujaya Premathilaka Mar 09 '20 at 16:21
  • You should search with the keywords : how to read a file from the class path. Any other kind of approach will probably lead you to a point where it will work on your computer / user session, but not on another computer. Check out : https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java. Please not that the fact that your project is eclipse based or that the file is XML has nothing to do with your issue. Your issue is "how to read a file that ships with my program". I've edited the question, you may roll back my changes if you like. – GPI Mar 09 '20 at 16:58
  • I don't understand where the file is supposed to be now. In your project in a config folder, wherever your user.dir system variable points at or somewhere else? – magicmn Mar 09 '20 at 17:18
  • the file is in the config folder @magicmn – Manujaya Premathilaka Mar 10 '20 at 03:55
  • I've mostly been using maven so I can't really tell you how to include files (or if everything that is in your project folders is automatically included) to your build process, but after you build your project you wanna check out if you're target folder/jar or whatever includes your configuration file. If that is true you can get an URL or InputStream (See https://stackoverflow.com/a/3844316/9712270) of the file. Then instead of parsing with a file you have to use the InputStream. – magicmn Mar 10 '20 at 07:44

0 Answers0