showing error near import statement - STS - java

I'm facing the issue when I imported the existing project in my STS (spring tool suite) workspace.
Below is the sample folder structure of my project:
Now dao folder has few java classes , utils folder has few java classes.
When trying to import classes from com.lpa.utils.* inside classes of dao folder its showing error (The import com.lpa cannot be resolved) near imports. Please suggest if I need to set anything in the build path.
Please see the folder structure all the classes are inside **src-- main -- java -- ..**. Because of that I was not able to navigate to other classes or methods using ctrl and mouse click.

It looks like you haven't imported the project as an existing Maven project (in case it uses Maven to build, the folder structure looks like Maven). If this is indeed a Maven project, you should use the "Import as Existing Maven project" import wizard. This will import the source folder structure that you have as "source folder".
Otherwise you would have to set this up yourself in the project properties, via the Java Build Path settings page.

Related

Eclipse project structure showing improper project structure

I have set up an eclipse workspace. Inside this workspace i have a dynamic web app cloned from repository. My working directory looks like following.
I am seeing a different hierarchy in Eclipse project explorer. My project explorer looks like following.
Ideally it should show src, WebContent, build.xml etc. inside java resources but it shows entire project folder inside java resources.
Troubleshooting I tried:
1. Delete existing project from project explorer and import the project again.
2. Pull the changes in local copy and refresh the project from project explorer.
3. Change the working directory and import a new project.
None of this seemed to resolve the problem.
Appreciate if someone could help me with the root cause and a solution to this.

Can't call a class from another git project in Eclipse

I imported a project from github into Eclipse (project A), using these steps: Importing a GitHub project into Eclipse
I have another app engine project I am building in Eclipse (project B). I would like to import a class from the project A to project B. I was trying to follow these steps: How can I refer to a class from another project in the same Workspace in Eclipse/Java
But when I go to project B's Java build path --> Projects --> Add, the select project window does not show the imported github project.
If I create another local Java project (project C), I can see project C as an option to add to project B's build path.
Make sure you are copied project A into your workspace while import into eclipse.
There is a check box option for copy project into workspace in eclipse import project.
First, You will have to fix the project A at eclipse before importing into the project B... By fixing, I mean setup the source paths of the project A (example, the folder src/com/google, should be a source folder at the project A configuration)... The Project A must compile (if the project A cannot be compiled by eclipse, I doubt that you will have success at importing it into project B)
Another strategy would be use the ant script of the project A and import, as java library, the artecfacts generated by those ANT files into the project B.

ClassNotFoundException from jar in Dynamic Web Project

I created a JPA project that I want to use in a dynamic web project.
I made a jar from this JPA project and added it in a lib folder in the dynamic webproject. I then added this to the buildpath of the dynamic web project and also ticked it in the "Order and Export" tab.
First question : I created the jar file with a normal export to jar with these options;
Are these ok or do I have to add something.
Second question: After I added this jar to the buildpath and I use a instance of a class from within this jar I can add it to the imports and the class is know so I don't see any errors.
When I try to run the jsp and servlet where I use the class I get a java.lang.NoClassDefFoundError error in my apache with a java.lang.ClassNotfoundEception at the line where I use the class. So for some reason he doesn' find the class at runtime.
I tried to make the jar over and over again with different options. I tried to import the class I need on different way's (ctrl-T) but I only see one class.
The only thing I find strange is the way the import looks. In my JPA project I have a package structure like :
be.JPA.project6
in my dynamic web project I have a package structure like :
be.project6
when I see the import, I see : be.JPA.project6.data.classname
I would expect that a different structure so he would know that he needs to search outside the "normal" package structure. But also with "ctrl-T" I only can see this class
I don't want to make a reference to the other project but use it like this with a jar.
I'm out of any clues at the moment, so any advise would be more then welcome.
The build path is that: a build path. A path used to build the application.
The runtime classpath of a webapp is composed from the WEB-INF/class directory, and the set of jars in WEB-INF/lib.
Remove the jar from the build path of the dynamic web project. The just drop it into the WebContent/WEB-INF/lib directory. Eclipse will automatically add all the jars in this directory to the build path, and will put them in the WEB-INF/lib directory of the deployed web application, so that they're also part of the runtime classpath.

Cannot import spring mvc project property into eclipse

I am trying to import the project at http://www.javaworld.com/javaworld/jw-04-2009/jw-springmvc-cms-src.zip into eclipse.
I do File -> New -> Project.. -> Java Project From Existing Ant Buildfile. I give the path of the build.xml of the project.
It imports the project into eclipse and shows the src and test folders. However, it does not show the WEB-INF, css etc. folders. Why? How can they also be viewed in the project structure in Eclipse.
You should try creating "by hand" the missing folders into your newly created project, then you can use Import -> File system to add the missing files into the respective folders...

Wrong package name when using automatically added imports in Eclipse

I have several Maven projects converted into one Eclipse workspace. When I edit the code under Eclipse, I sometimes use CTRL+SHIFT+M combination of keys to automatically add imports to the classes from a project in my workspace. But somehow they are added like this:
import src.main.java.com.mycompany;
while to real package I wanted to import is com.mycompany.
This has to be some configuration in Eclipse to fix this but I'm confused. However, this issue is very annoying.
EDIT:
I've forgotten to mention that Eclipse files were generated using mvn eclipse:eclipse command.
Under Eclipse project seems to be configured properly. Its source folders set like this:
src/test/java
src/test/resources
src/main/java
src/main/resources
And everything under Eclipse works properly except the situation when I press CTRL+SHIFT+M keys
The standard source folder for Java projects is
./src
For imported maven projects, simply remove this folder from the list of source folders at the build path settigs. The correct source folder is
./src/main/java
In eclipse;
Remove the existing source folders first.
-right click -> menu -> build path -> remove from build path
then
Right click on the source folder.
build path -> use as source folder.
Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.
It's because eclipse is not aware of the convention over configuration filestructure Maven is following. Install the M2Eclipse plugin and File > New > Other > Maven Project for new projects or for existing ones right click on your imported project on Package Explorer > Maven > Enable Dependency Management. Once successfully done, on the Package Explorer you would see your project nicely gathered following the Maven conventonal filestructure like src/main/java, src/main/resources, src/test/java and from then on you'll start seeing your package structure hierarchy like com.mysite.myproject..
First remove it. Then add it back using right click on package->build path->configure build path-> Source->Add Folder and add the entire /src/main/java tree
You've got the wrong source folders in your build path, and it's a wonder that anything works at all.
You can use either the maven-eclipse-plugin or M2Eclipse to automate getting this right, or you can manually fix the build path to call our your actual source folders, not their great-grandparents.
Non of the above worked for me. Finally I just changed the name of the scr folder to scr-java and this removed the package structure with scr as root package.

Categories

Resources