4

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:

  1. Is my understanding correct?
  2. If so, how it does to find the classes it is loading?
  3. 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?

Community
  • 1
  • 1
Alexandre
  • 1,016
  • 3
  • 13
  • 23

7 Answers7

9

Two possibles cases for me :

  • You are using eclipse to compile your project. Then eclipse is configured to exclude (or not include) resources in the src folder. You can set it in Project/Properties/Java Build Path/Source. Then you expend your src folder, and ensure you have something like "Included All", "Excluded None".

  • You run your maven application using maven to compile and not eclipse, even though you are using eclipse as your IDE. Then by default maven will not copy resources from the source folder to the output folder... Because it is not the standard maven way of doing things. Thus the resource are missing from the classpath and you don't find them. Just change your maven configuration to also include resources from your source folder.

Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18
6

I think you have to add src/main/resources to the build path. This is done in Project Properties > Build Path > Source. Here is how the standard maven project looks:

enter image description here

In future when using Eclipse I suggest to use m2eclipse plugin and create project using it. This will automatically make sure that all these folders are in the build path.

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
2

Try this: Run configurations... -> Classpath -> User Entries -> Advanced... -> Add Folders

jilt3d
  • 3,864
  • 8
  • 34
  • 41
1

Can you try loading the resources using below?

this.getClass().getClassLoader().getResourceAsStream(propertyFileName)
Prabhjot
  • 695
  • 3
  • 8
  • 21
0

For me the Files within the WEB-INF were not getting included. Hence I added them to Buildpath (Project -> Build Path -> Configure Build Path -> Add folder (project/src/main/webapp). This resolved the issue.

0

This might be useful information

Eclipse Maven plugin has its own Classpath Container that conflicts with generated class paths when enabled.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
0

What I would suggest is stop using eclipse:eclipse (sorry - harsh I know). I used it for about 8 months, cant believe it took me that long, and used M2Eclipse. M2Eclipse is an eclipse plugin in which you do the following.

  1. Enable Dependency Management.
  2. Update Project Configuration
  3. (In Eclipse) Project -> Build Project
  4. (In Eclipse) Run Application

It may not seem it, but its a much easier and less frustrating way of doing it.

Before, with eclipse:eclipse. You would have to run it then hit refresh and hope that everything is configured - if you have a multi-module pom things can really go wrong.

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • I've also found that Eclipse doesn't play nice with build tools other than its own, since Eclipse uses its own internal build mechanism. I would also suggest using M2Eclipse, as it makes the build in eclipse a little less frustrating. Netbeans integrates builds with maven much better, but that's not always an option. – BuffaloBuffalo Apr 05 '11 at 12:57