1

I am using this code for import shutdownscheduler.xml file from my package but I am getting error:

public static Document handler() throws ParserConfigurationException, SAXException, IOException{
        String s=xmlhandler.class.getResource("shutdownscheduler.xml").toString();
        File newFile=new File(s);
        DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder documentbuilder=documentbuilderfactory.newDocumentBuilder();
        Document document=(Document)documentbuilder.parse(newFile);
        document.getDocumentElement();
        return document;
    }



Exception in thread "main" java.io.FileNotFoundException:
C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown
Scheduler\file:\C:\Users\Rohan%20Kandwal\Documents\NetBeansProjects\Shutdown%20Scheduler\build\classes\shutdown\scheduler\shutdownscheduler.xml
(The filename, directory name, or volume label syntax is incorrect)

If I give a direct path of the file then the import is working correctly. But this will result in an error once 1 create a jar file of the project and run it on other system. So I want a way to import file from my package only.

the correct path for the file is C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler\shutdownscheduler.xml

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107

1 Answers1

2

Try InputStream instead of File

documentbuilder.parse( xmlhandler.class.getResourceAsStream("shutdownscheduler.xml"));
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 1
    @RohanKandwal so you can accept the answer as correct with the tick – StanislavL Jan 15 '13 at 08:02
  • The code is working correctly but the main problem arrives when i use this code for saving a xml file. getResourceAsStream() uses path "C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\build\classes\shutdown\scheduler\shutdownscheduler.xml" but the path should be "C:\Users\Rohan Kandwal\Documents\NetBeansProjects\Shutdown Scheduler\src\shutdown\scheduler\shutdownscheduler.xml" – Rohan Kandwal Jan 16 '13 at 11:12