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.
Goal: 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).
Current setup: 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).
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:
- src
- java
- main.package.com
- subpackage
- LookUpClass.java
- PlotterClass.java
- MainClass.java
- subpackage
- main.package.com
- resources
- LookUpTables
- LookUpTable1.txt
- LookUpTable2.txt
- LookUpTables
- java
I wrote a method in LookUpClass.java
that is retrieving a certain line from my lookup tables in resources. To retrieve the file and read out a certain line, I used
// 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 < line; ++i)
br.readLine();
return br.readLine;
}
In my project structure the "java" folder is marked as "source", while "resources" is marked as, well, "resources".
My test setup is very simple:
public static void main(String[] args) throws URISyntaxException, IOException {
String c = LookUpClass.getLineFromLUT(5);
System.out.println("Color of line 5: " + c);
}
Output:
Color of line 5: 0 0 38
(Which is correct.)
I added the exact same lines to PlotterClass.java
and it works fine, too.
Problem:
Now, If I try the same in MainClass.java
I get an error with url
being null. It seems the resource/resource folder can't be found.
I read through various postings on SO already and tried out several proposed solutions, which all failed so far:
- If using
LookUpClass.class.getClassLoader().getResource("/LookUpTables/LookUpTable1.txt")
both callings fromMainClass.java
andLookUpClass.java
fail (url
is null). - 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"
- Since using Idea IntelliJ, I checked "Settings > Build, Execution, Deployment > Compiter > Resource patterns" and added "*.txt" to the patterns. Nothing changed.
- If adding
Class c = LookUpClass.class.getClass();
, in Debug mode c is "class.java.lang.Class". I was expecting something like "main.package.com.subpackage.LookUpClass". - At some point I tried using
getResourceAsStream()
, 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.
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 LookUpClass.java
is run from a different location than MainClass.java
. 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?
try { Class.forName(MainClass.class.getName()); } catch (final ClassNotFoundException ex) { }
– RepairgetLineFromLUT
, nothing happens, when executing. So I guess the MainClass is actually found. ExecutedMainClass.java
as well asLookUpClass.java
. – VoletaURL url = LookUpClass.class.getClass().getResource("resources/LookUpTables/LookUpTable1.txt");
this works for me as long as the project is not run as a jar. – Repair