I'm working on a swing project, using maven2 (from command-line) and eclipse (without maven integration). So, I generate the eclipse project through maven eclipse plugin (mvn eclipse:eclipse
), import it inside eclipse, and do all my work.
My problem is: when I run my app in eclipse (as a Java Application), I can't find none of the resources that are in my src directory. Digging for information on my problem, I get into this answer from another question. So, I compared the output from the following instructions:
MyClass.class.getResource("/").getPath();
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toString();
Those gave me the following outputs, respectively:
${workspace_loc}/${my_project}/target/test-classes/ file:/${workspace_loc}/${my_project}/target/classes/
Checking the above locations, I could see that the former is empty, while the other one contained all my compiled classes and resources. So, I came to the conclusion that the classloader is looking for my resources in the wrong place. So, I think I have three questions:
- Is my understanding correct?
- If so, how it does to find the classes it is loading?
- How do I solve this?
UPDATE: I've changed my code, so instead of invoking MyClass.class.getResource(...)
or MyClass.class.getResourceAsStream(...)
, I'm now using ClassLoader.getSystemResource(...)
and ClassLoader.getSystemResourceAsStream(...)
. In this way, everything is working fine in eclipse. I just don't know exactly why. Any hint on this?