title: Retrieving resources for a subpackage class
body: <p>I haven't been working with java for long, so I'm not sure as what else to look for. I hope somebody can point me in the right direction.</p>
  
  <p><strong>Goal:</strong> 
  I want to use a look up table, stored as a text file. But I don't want to use absolute paths, as in the end I'd like to pack a release and be able to call it from any location (the text file will be included int the packed release).</p>
  
  <p><strong>Current setup:</strong> 
  I put the text file in a folder called "resources" (because from reading tutorials about java, I got the impression, this is where I'm supposed to put it to maintain a better structured project).</p>
  
  <p>In the root package folder I have a class (MainClass.java) that is calling another class (LookUpClass.java) in a subpackage.
  The folder setup is as followed:</p>
  
  <ul>
  <li>src
  <ul>
  <li>java
  <ul>
  <li>main.package.com
  <ul>
  <li>subpackage
  <ul>
  <li>LookUpClass.java</li>
  <li>PlotterClass.java</li>
  </ul></li>
  <li>MainClass.java</li>
  </ul></li>
  </ul></li>
  <li>resources
  <ul>
  <li>LookUpTables
  <ul>
  <li>LookUpTable1.txt</li>
  <li>LookUpTable2.txt</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I wrote a method in <code>LookUpClass.java</code> that is retrieving a certain line from my lookup tables in resources. To retrieve the file and read out a certain line, I used </p>
  
  <pre><code>// Gets respective line from LUT
  private static String getLineFromLUT(int line) {
      URL url = LookUpClass.class.getClass().getResource("/LookUpTables/LookUpTable1.txt");
      File file = new File(url.toURI());
      BufferedReader br = new BufferedReader(new FileReader(file));
  
      for (int i = 0; i &lt; line; ++i)
          br.readLine();
  
      return br.readLine;
  }
  </code></pre>
  
  <p>In my project structure the "java" folder is marked as "source", while "resources" is marked as, well, "resources".</p>
  
  <p>My test setup is very simple:</p>
  
  <pre><code>public static void main(String[] args) throws URISyntaxException, IOException {
      String c = LookUpClass.getLineFromLUT(5);
      System.out.println("Color of line 5: " + c);
  }
  </code></pre>
  
  <p><strong>Output:</strong> </p>
  
  <pre><code>Color of line 5: 0  0   38
  </code></pre>
  
  <p>(Which is correct.)</p>
  
  <p>I added the exact same lines to <code>PlotterClass.java</code> and it works fine, too.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Now, If I try the same in <code>MainClass.java</code> I get an error with <code>url</code> being null. It seems the resource/resource folder can't be found.</p>
  
  <p>I read through various postings on SO already and tried out several proposed solutions, which all failed so far:</p>
  
  <ul>
  <li>If using <code>LookUpClass.class.getClassLoader().getResource("/LookUpTables/LookUpTable1.txt")</code> both callings from <code>MainClass.java</code> and <code>LookUpClass.java</code> fail (<code>url</code> is null).</li>
  <li>I tried using following paths (all not working in either of the classes):
  "LookUpTables/LookUpTable1.txt" (removing starting "/")
  "/subpackage/LookUpTables/LookUpTable1.txt"
  "../subpackage/LookUpTables/LookUpTable1.txt"</li>
  <li>Since using Idea IntelliJ, I checked "Settings > Build, Execution, Deployment > Compiter > Resource patterns" and added "*.txt" to the patterns. Nothing changed.</li>
  <li>If adding <code>Class c = LookUpClass.class.getClass();</code>, in Debug mode c is "class.java.lang.Class". I was expecting something like "main.package.com.subpackage.LookUpClass".</li>
  <li>At some point I tried using <code>getResourceAsStream()</code>, but I didn't understand how to get my (e.g.) 5th line, so I discarded it. I'm willing to read up on this, if it solves my problem though.</li>
  </ul>
  
  <p>I have no idea how to solve this problem. And I realize that at this point I'm just trying out things, not even understanding why it could or could not work.
  For me, it just seems <code>LookUpClass.java</code> is run from a different location than <code>MainClass.java</code>. But the "resources"-folder and respective text file location never change. How can the file be found in one case, but not in the other?</p>