0

I have some problem in my eclipse settings. I am trying to open/use a XSD file in my class but it doesn't seem to find it as the code isn't using relative path. When I copy the absolute path from my computer and paste it, it seems to work. But I need to use relative path as this project will be exported and may be used by others.

This is what Im trying to do: Schema schema = sf.newSchema(new File("myfilename.xsd"));

This is what I get in the console: Failed to read schema document 'file:/Applications/Eclipse.app/Contents/MacOS/sisrep.xsd, because 1) could not find the document;

When I do Schema schema = sf.newSchema(new File("Users/username/etc/etcabsoultepath/myfilename.xsd")); it works fine, so im guessing eclipse isn't using the relative path.

Also, this code is run on a servlet. I had imported a sample java application file and run a similar operation, and that file was getting the relative path as needed, but my project isnt able to. Is there any setting I need to change or specify in eclipse for my project? I tried add .\\ before the file name but it didnt seem to work.

  • 1
    A *relative path* is relative to the *working directory* of the process, i.e. of the running Java program. Why do you believe that the *working directory* is `Users/username/etc/etcabsoultepath` when running from Eclipse? --- *"this code is run on a servlet"* What do you believe the *working directory* of the Servlet container (e.g. Tomcat) is, and why do you believe that? Servlet code should **never** rely on the *working directory*. – Andreas Nov 26 '20 at 23:22
  • Add the file as a resource and read it from the classpath. It will be much easier to work in different environments this way. See [here](https://stackoverflow.com/q/15749192/2587435) – Paul Samsotha Nov 26 '20 at 23:23
  • 1
    Servlets shouldn't access *on-disk* resources outside of the web application. Put the schema in your application. – nitind Nov 27 '20 at 04:25

0 Answers0