Problem loading resources while running in Eclipse - java

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?

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.

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:
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.

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

Can you try loading the resources using below?
this.getClass().getClassLoader().getResourceAsStream(propertyFileName)

This might be useful information
Eclipse Maven plugin has its own Classpath Container that conflicts with generated class paths when enabled.

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.
Enable Dependency Management.
Update Project Configuration
(In Eclipse) Project -> Build Project
(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.

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.

Related

Problems with IntelliJ at cloning repo from github

I am new to github. I tried to clone a repo and then pull the files on my computer. All the other team members did the same thing and it worked for them. When i open the project with Intellij all the .java files look like this:
files
It also says that my SDK isn't set.
I tried to set my SDK, and also i tried to set the java directory as 'sources root' because someone said that is a problem with the project structure.
These problems disappeared but then IntelliJ didn't recognize that my project is a maven project so i had to add manually the file pom.xml and refresh maven.
Then this problem disappeared but i had another:
"Cannot start compilation: the output path is not specified for module 'cmsiss'.
Specify the output path in Configure Project."
I tried to set it from project structure but now I have this error:output error
Please help.
Open maven project using pom.xml file. File->Open->pom.xml
Sources should be pointed to project root, not java directory.
Give these a try.
The usual steps that I follow to open the project:
git clone <github clone url>
Open the project in intelli J using
File<Open
and browse to open the root of the project.
This should work fine but if it does not, a few more things you can try:
Try making the project root as a maven module
You can open the Project Structure to make sure you have Java SDK set up properly. It should show the java version you are using there.
Worst case, that is if nothing else works and if the project is not too big, just create a new maven project and make sure it runs, then add all the project files.
I pulled the project again and started from zero. I specified the SDK and then I opened the Maven project window and added a maven project (selected the pom.xml file) and refreshed it. Maven did it's job and created the project structure and all the stuff I've tried to do manually and finally worked!

adding jar file dependencies

I have a Maven project that I've taken over and imported to Spring Tool Suite.
I added the Java facet so I could use "Open Declaration" and "References" to find out info about various classes used in the code.
The problem is, now I've got all kinds of errors because jar files (managed in the pom files) aren't on the Java build path. I've got them all in my .m2 directory but really don't want to add them by hand (there are about 100 or more buried in a deep directory structure)
Do I have any options to detect automatically or something that will prevent me from adding them 1 by 1?
Try this:
Select your project in STS (Sprint Tool Suite)/ Eclipse
-> (Right-Click)
-> Maven
-> Update Project (shortcut Alt+F5)
-> (Check all, except 'Offline' option) and press Ok
It will rebuild your project and hope solve your problem.

Remove exclusion of resources folder from eclipse build?

I imported my existing maven projects under eclipse.Once imported i found under Java Build Path > Source resource foulder is
excluded(i can see Excluded:**) from build and does not get copied under target > classes directory.
I can remove exclusion manually by selecting Excluded:** and then click Remove, But there are large numbers of projects. Is there
a setting in eclipse where i can do it one go ?
That's part of what Maven does when you let it generate your Eclipse project settings (via the eclipse:eclipse plugin/goal). It's quite misleading. However, if you have the m2e plugins installed into Eclipse it coordinates things correctly.
What's going on, as best as I can figure: Eclipse's built-in Java builder automatically copies non-Java files found on the build path to the output location (unless they're excluded as you see in your Build Path). Problem is, when Maven comes in the picture it, too, wants to copy resources during a build. So to keep the two builders from competing/interfering with each other, Maven instructs Eclipse to ignore resources so it (Maven) can manage copying them during a build.
m2e integrates into the Eclipse build process to do the Maven copying of those files, so if you configure the project as an m2e project things will "just work," both in Eclipse and if you build from a command line.
I don't know of a way to tell Maven to not configure the project this way. To be honest, that's just one of the reasons I prefer to not let Maven generate my Eclipse project files; I do a much better job of it and I totally control the specifics rather than relying on whatever the Maven eclipse developers think is best.
This is an old question, but I've been looking for the answer and I finally dug it up myself. According to the M2Eclipse FAQ the import intentionally excludes resources. This is because Maven performs special resource handling that includes filtering.
Let Maven do the building for you. Disable automatic building in Eclipse and never build your projects in Eclipse.

Missing required library in eclipse for non-required library

I'm using Eclipse for Java Develepers (Juno x64 running on Windows 7).
I have a project in which I was trying out a jar I no longer need (args4j), so have removed the dependency. At some point, my PC lost power unexpectedly, and I've since restarted Eclipse.
My project now has an error, "Project 'MyProject' is missing required library: '/path/to/args4j-2.0.21.jar'". I was working with the project for some time after removing the dependency without any issues.
Under the Project Properties > Java Build Path, there's no reference to args4j in any of the tabs.
I've tried:
searching for the string "args4j" in all files within my project, but there are no matches. There are a bunch of references within <workspace>\.metadata\.plugins\org.eclipse.core.resources\.history\, but I'm reticent to touch them out of fear I might break something else.
[Edit] clean rebuild has no effect.
Does anyone have any idea why Eclipse is complaining, and what I might be able to do to fix it?
Please do the following, if one does not solve your problem, skip to the next one:
Check .classpath file in your project folder. You might have a reference there.
Close and re-open the project without closing eclipse, this might rebuild Eclipse resource cache.
Make a clean build after re-opening.
Re-import your project into a clean workspace.
Re-adding the problematic jar, then removing it did the trick. This is better than reconfiguring the entire workspace in my case, as I have many projects, and ADT configuration in my workspace.
I've found that the easiest and simplest way is to switch to the navigator view, open the .factorypath file and delete the offending dependency.
Deleting .classpath and .project, then reimporting the project as a Maven Project did work for me. It may also be useful to mark all Eclipse workfiles like .classpath in the p4ignore or gitignore from the beginning at the initial commit.
DO - mvn eclipse:clean eclipse:eclipse, on project and then refresh and then clean project , will solve this problem

How to create an automated setup for projects using Eclipse (like Ant)

It is the following situation:
I currently have multiple Java projects in Eclipse. All the sources, build files etc are checked in at a repository. If a new team member joins the project he has to rebuild the complete setup (setting build path dependencies, adding special libraries).
I thought if that could be automatically done some way, e.g. using an ant file to do all the configurations of eclipse or the servers (tomcat).
Anyone ever found a solution for this?
Check in the .classpath and .project files and you should be set!
(More information about the .project file.)
I believe that you might use Maven too.
But that would require quite an adjustment, I believe.

Categories

Resources