2

Currently, I can only make my app access to the XML database when the jar file and XML file in the same directory, and what I hope is that there is only one single jar file which includes the XML file. How can I do that?

private String getXmlPath() {
    String url = "";
    try {
        String cp = "Data\\QuestionList.xml";
        File f = new File(ClassLoader.getSystemResource("\\").toURI().getPath());
        url += f.getPath() + "\\" + cp;
    } catch (URISyntaxException ex) {  
        url="QuestionList.xml";
    }
    System.out.println(url);
    return url;
}

public void classifyQuestionnaire(String path)
        throws FileNotFoundException, XMLStreamException, JAXBException {
    // Parse the data, filtering out the start elements
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    FileReader fr = null;
    if (path == null) {
        fr = new FileReader(getXmlPath());
    } else {
        fr = new FileReader(path);
    }
    XMLEventReader xmler = xmlif.createXMLEventReader(fr);
    EventFilter filter = new EventFilter() {

        @Override
        public boolean accept(XMLEvent event) {
            return event.isStartElement();
        }
    };
loql
  • 271
  • 1
  • 2
  • 9

1 Answers1

1

You can include xml file into the jar. These non-class files are usually called "resource". It depends on how you're creating the jar, but it's usually straight forward. See Java: Load a resource contained in a jar for reading the resource.

Community
  • 1
  • 1
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319