title: Java Access File Outside of Package via Relative Path to load as Image (JavaFX)
body: <p>I am attempting to make a simple menu in JavaFX. When I attempt to load an image using a relative path I receive the following exception:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
  </code></pre>
  
  <p>I am using eclipse, although this problem appears not be eclipse specific, since I have received the same exception when running in terminal. Also, for some reason it works when the file is in inside the package or in the src folder (given path is changed appropriately).</p>
  
  <p>My Current active class <code>Menu.java</code> is in the package <code>test.dsp</code>, which is located in the project as:</p>
  
  <blockquote>
    <p>project/src/test/dsp/Menu.java</p>
  </blockquote>
  
  <p>The file I am attempting to access:</p>
  
  <blockquote>
    <p>project/assets/graphics/test/dsp/sslogo.png</p>
  </blockquote>
  
  <p>I am attempting to access the file under the assumption that this path is relative to the current <code>Menu</code> class in <code>Menu.java</code> and due to the physical directories being <code>project/src/test/dsp/Menu.java</code>, I am implementing <code>../</code> thrice in my relative path to get to the project directory. Then I simply put the path to the image as seen below.</p>
  
  <pre><code>Image ssLogo = new Image("../../../assets/graphics/test/dsp/sslogo.png",
              2000, 2000, true, true);
  </code></pre>
  
  <p><a href="https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html" rel="nofollow">I am basing my knowledge off of the constructor described in the JavaFX API:</a></p>
  
  <pre><code>public Image(java.lang.String url,
       double requestedWidth,
       double requestedHeight,
       boolean preserveRatio,
       boolean smooth)
  </code></pre>
  
  <blockquote>
    <p>url - the string representing the URL to use in fetching the pixel data</p>
  </blockquote>
  
  <p>I have searched through a plethora of questions but their answers do not provide a solution to making my current case work. I've tried getResource, getResourceAsStream, and external resource. I checked the URL that is returned by these means and none worked with my provided path as seen above. I also tried different numbers of <code>../</code>, still to no avail. Admittedly, I may have implemented them wrong, which led to no success.</p>
  
  <ul>
  <li><p>Why does this method of file access via relative path not work and how can I make this work with my current directory set up?</p></li>
  <li><p>What is the proper way to access a file via a relative path in java in general? And specifically when the file is outside of the package, such as this case?</p>
  
  <ul>
  <li>is there a way to include the file into the file path via code in the java class?</li>
  </ul></li>
  <li><p>Is it more efficient to have the file inside the package, or does it not matter (clearly depending on the means of accessing the file outside of package)?</p>
  
  <ul>
  <li>is there a more effective means of file access through java?</li>
  </ul></li>
  </ul>