0

I want to load an XML into my java program from my classpath using a URL.

     URL resource = MyClass.class.getResource("test.xml");
     File file = Paths.get(resource.toURI()).toFile();

The catch is that I want to be able to do this without giving the full path to the xml file. How can I properly add the XML to my classpath to test this. I thought I added it correctly as an external folder but I'm getting a null pointer exception indicating that it was not added properly or that I can't actually search for it.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
Bharat M
  • 109
  • 7
  • The full path wouldn’t work, regardless. The argument to getResource is not a file name, it’s a relative URL, whose base is each entry in the classpath. So, what is your classpath? And what is the name of the package containing MyClass? – VGR Jun 20 '17 at 19:35
  • C:\MyWorkspace\Project1\Source\Java\org\Package1\MyClass.java where org\Package1 is the package – Bharat M Jun 20 '17 at 19:42
  • The Class.getResource method expects the specified name to be in the same package. Is test.xml in an org/Package1 directory? – VGR Jun 20 '17 at 20:05

0 Answers0