Unable to solve import classes in maven project - java

I am developing a maven project using IntelliJ-14. The other day everything was correct without any problem but today when I got in the project some of my imports are not working. When I compile it is everything ok and I can run the project but autocomplete does not work and some classes appear red.
I've tried to go File-> Settings-> Maven-> Importin-> Import Maven Projects Automatically check. Already imported the project again and nothing. In external libraries classes exist.
An example of my code is here.
What else can I do to solve this?
Please help

Right click on the project-->Maven--->UpadateProject.
This may help you.

Some possible solutions since we can't tell what is actually wrong:
Delete your .m2 folder and do a reload maven project
Go invalidate "caches and restart" (File - Invalidate caches /
Restart)
If you work with other people see if they have committed something that breaks the maven build. Some times the version of jars that you have in your local maven repo (.m2 folder in your %USER_HOME% folder) is different than the one that they other devs using
Do "Reimport all maven projects"

I experienced the same problem. Before it occured, I created a package structure from within IDEA, for example "de.ulli.myPackage". Of course I expected to get the directory structure "de/ulli/myPackage". I don't know why, but actually there was a single directory named "de.ulli.myPackage" (OS Windows 7 64 Bit). Maven was able to build the project, and I even could generate classes via IDEA. But autocomplete didn't work and the classes within the package appeared red in other sources. After reparing the directory structure, everything was fine.

Related

IntelliJ cannot resolve any symbols or methods

IntelliJ IDEA 2017.2
I know this has been asked, but I have tried every fix I could find, as shown below.
Every symbol in my java code has an error Cannot resolve method, or Cannot resolve symbol. However, the code still compiles and runs with 0 errors. The error messages in the IDE started the first time I opened the project, on 3 separate computers, but runs fine on a fourth....I can't find anything different on that fourth computer.
I have following the following instructions from this issue:
File -> Invalidate Caches / Restart
I deleted the [IDEA system](https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs) directories, and re-imported the Maven project.
I have re-imported the Maven project
I re-imported the project with auto-import
I also followed the instructions from this issue:
File -> Synchronize
then
File -> Invalidate Caches / Restart
As well as the solutions here:
Check source roots
Ctrl+Alt+Shift+S
There were no errors or fixes in the Module source roots, or the module dependencies.
I also tried re-importing with "Search for projects recursively" checked as recommended here.
Edit1
This isn't a screenshot from my actual code, but my errors look the same as the screenshots from this post.
EDIT2
I just found an error I hadn't seen before. When I go to Project Structure (Ctrl+Alt+Shift+S), under Libraries, I see the following errors.
I checked on the location shown /home/user/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7-sources.jar and it doesn't exist. I have antlr-2.7.7.jar, but I'm not sure if I need to download the antlr-2.7.7.7-sources.jar, or just point my Library to the .jar I have (as I'm unfamiliar what these settings do)
The answer was the look through the pom.xml file line by line, and see what the dependencies were that I didn't have. I thought it was a configuration issue, but it wasn't.
I didn't notice that I didn't have the lombok plugin, which was a dependency for my project.
The problem listed in EDIT2 wasn't actually an issue since all of the classes were available.
Since your project compiles and runs successfully with mvn clean install, you know your pom is valid.
Given that, I would recommend to:
CloseIntelliJ
delete the .idea directory and any *.iml, *.ipr files for each of your modules. Don't worry, IntelliJ will re-create them. Back them up by renaming them if you're really concerned.
Open IntelliJ and find the new project wizard.
Find the option to import from an existing maven project
Select the pom for your root project
Check if module settings are correct. Open Project structure (Ctrl+Alt+Shift+S) and check the source directory settings under Source Tab are valid.
This is a weird one. I faced the same issue and went through all questions on stackoverflow and other forums and applied all suggestion to an extent to reinstalling intelliJ.
Though what problem in my case was that i had excluded some files namely .gradle and build folder, which definitely sounds important. I must have done this get excluded for git status notifs but somehow i had excluded it from my project as i am new to intellij. To check if any of your folders are exclude goto -> modules CTRL + Shift +Alt + s and check for any red items you should probably include the ones necessary here.
Hope this helps few of those out there.
you are using maven project, delete .m2 folder and update maven project and clean the project the rebuild it and run it... some time maven project occur that type of issue, which resolve by rebuilding the project. also check the version of your dependency.

IntelliJ cannot find any declarations

I completely uninstalled IntelliJ and have now reinstalled and imported my project. I imported using gradle and can see all of the files in my project present. However, when I open a file I can't find any declaration to go to by doing cmd + click. I have the Java 7 SDK setup, and I can't even go to the declaration of classes in my own project. Going to a Java core class like String or List doesn't work either. How can I enable the IDE to be able to find my classes?
I'm on OSX Yosemite, IntelliJ 2016.1.2 build 145.972.
I had this same problem, and #AniaG's solution in the comments worked for me.
Right-click src folder
Mark Directory as > Sources Root
I faced the same issue and spent almost 15-16 tiring hours to clean, rebuild, invalidate-cache, upgrade Idea from 16.3 to 17.2, all in vain.
We have a Maven managed project and the build used to be successful but just couldn't navigate between declaration/implementations as Idea couldn't see the files.
After endlessly trying to fix this, it finally dawned to me that it's the IDEA settings causing all the headache. This is what I did (Windows system):
Exit IDE
Recursively delete all .iml files from project directory del /s /q "C:\Dev\trunk\*.iml"
Find and delete all .idea folders
Delete contents of the caches, index, and LocalHistory folders under <user_home>\.IntelliJIdea2017.2\system
Open Idea and import project ....
VOILAAAAAAAAAAAA...!!
I hope this helps a poor soul in pain
I had the same issue with idea and I was trying to open a maven project but the pom files where not identified. So right clicking on the pom file and choosing "add as maven project" did all the magic for me :)
Had the same problem. But only on my own methods.
Just fixed it by invalidating cache: (File-> Invalidate Caches/Restart)
I was having similar issues in my IntelliJ mvn project. Pom.xml was not recognized.
What worked for me was right click on the pom.xml and then add as a maven project.
Most of the times there has been a problem with building the cache of the IDE, or something related.
Most probably, File -> Invalidate Caches / Restart, will resolve this problem, just let the IDE finalize it's caching.
Your source folders where your Symbols are (Classes) are need to be configured as "Content Root".
Open Project Structure: #;
Click Modules
You can configure your Content Root (Sources, Tests, Resources etc) for each module that you want to Navigate to.
Once done you should be able to navigate to your symbols.
If you see red circle on the icon of Java files, and got warning like "cannot find declaration to go to". Then you can do something below:
Right click root folder of your project/module.
Mark Directory As -> Excluded.
It works for me. in Windows 7, Intellij 2016.1.1
Right-click on your src folder and choose the option "mark directory as" --"Source root" and along with it again right-click on your project and choose "reformat code"(under this choose cleanup code too) and that will work fine.
I got this problem on Big Sur.
Solution:
File -> Invalidate Caches. After that press Option + Enter on every red field.
In my case, I just updated my IntelliJ to Ultimate 2018.2 and all of my projects suddenly cannot find the implementations and the 'src' folders - it turned out IntelliJ removed the type of project (e.g. Maven).
What I did is:
Right click on the root project > Add Framework support... > Look for Maven (in my case) > Wait to re-index again > Then it worked again.
UPDATE 2:
I have always been encountering this when I update IntelliJ (2019.1.1 Ultimate Edition).
Just click the refresh button of Maven Tab and it should re-index your current project as Maven Project:
I found this cannot find declaration to go to problem once in my Maven project. The reason for this was just that one of the sub-projects in my project did not import as maven project correctly.
None of the methods listed in this channel works for me.
However, I am able to resolve it using the following method, and I hope that it works for you too.
Right Click the pom.xml file and then select "Maven". After that, click "Import Maven Project"
Ensure that the directory which contains your compiled classes and libraries is marked as sources Root.
I too faced this issue. I've tried the solutions mentioned here. The issue seems not with the source folder.
For me the issue occurred when I installed a new version of IntelliJ, was using 2019 version moved to 2020 version.
The project got opened in the new version but the declarations were missing.
I fixed this by :
- File>Project Structure.
- Under Project Settings go to Modules. Here you should see the different project folders, for me it was not there.
- Click the + button on top and click Import Module.
- Select the root pom.xml and wait for the indexing to complete.
After the indexing is done all the declarations were working.
Importing my project (which had 3 different modules in it) - as File -> Module From Existing Sources fixed it for me.
Just run below commands
mvn idea:clean
mvn idea:idea
IDEA may ignore some of your maven dependency files. The "External Libraries" node in your project structure might be empty or incomplete.
Go to:
IntelliJ --> Preferences
Search for Maven [on the left tab] -> Ignored Files
See if there is any path on the right side that is checked(ignored) and uncheck that.
Came across the same issue and in my case (Java project), I had to include all the dependent jars in the project's libraries section.
File -> Project Structure -> Libraries
I had to add my project dependent jars in the above section (for example; project/web/lib/). After doing so, all resolved fine. I hope this will help someone.
For someone whom the above solution didn't worked.
For gradle,
File -> Project Structure
Click on Project Under Project Settings
Update Project Language level as Modules, private methods in interfaces etc.
Click OK and Import Gradle Settings
I had similar issue with Goland JetBrain and what worked for me is similar to what #user8382868 said but I only closed the IDE, renamed the folder .idea file to .idea.old and after re-opening the project everything worked.
For Java based project, this is what worked for me :-
-Download Intellij 2020.3.4 version instead of 2021.1.1
-Add configuration, plugins.
-Configure settings, Project Structure
-Right click on the root folder -> Mark Directory as -> Sources Root
-File -> Invalidate caches/restart -> Invalidate and Restart
In my case I was using a gradle project with subprojects. The reason IntelliJ couldn't find declarations was that the subprojects were not built.
After investigating why subprojects were not built, I found out that using "auto-import" was breaking the subprojects.
After importing the project from build.gradle without auto-import, it worked for me.
For what its worth, in Pycharm it is: Right click on the root folder->Mark Directory as-> Sources Root
Same Problem for Python Project!!!
I tried to make a clean new intellij project and updated everything. Tried different virtualenvs but nothing worked out.
UNTIL I uninstalled the Python Plugin + Restart IntelliJ + Intall Python Plugin + Restart IntelliJ again. I'm not sure if this is a "real" solution, but it solved my issue.
Maybe you can try something familar for Java "Plugin"/JDK?
In my case, none of the above helped. Only upgrade of Intellij from 2017.3 to 2018.2 helped.
Do not try 2018.1 as it had a bug wherein the Find files popup would not come up.
Somehow I set my Maven modules to be ignored. In the Maven tool window right click and select 'Unignore Projects'.
Then all the directories will be automatically selected.
I had the same issue.
what i did was.
Select your project >right click> go to settings> plugins > search for cucumber > and then re-bundled the plugin
and restart your intellij.
It should fix the issue.
Nothing above worked for me unfortunately. I deleted all downloaded dependencies from .m2\repository and did a mvn clean install to download them again. (Although i did not have any issues in the pom.xml at the first place)
That worked for me.
In my case, I've to open preferences:
And I went to Project -> Project Structure -> set my folder project in +Add Content Root
So now all it work fine!

Project is missing required library [Eclipse]

I have imported a java project into a new eclipse workspace and it gave me a lot of errors:
It says "Project 'myProject' is missing required library: /User/linus/.m2/..." (The directory is longer but not of interest for you)
Now, I have looked inside my Finder to see what is in ".m2" but there is no such directory where it should be. Additionally I let it show all the hidden files but no success. Lastly I tried to get into the folder with the terminal (using cd and then the directory eclipse gave me) but that did not work either.
I saw this post but it did not help me.
I reinstalled Maven to make sure it can be used, this is the output if I type mvn --version and mvn in the terminal:
Does anyone have an idea?
Thanks in advance
EDIT:
I reinstalled Maven and now there is a /.m2 folder. It contains /.m2/repository/ but there is nothing in it...
May I suggest you read a Maven tutorial like http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
IMHO it's a wonderful build tool because it gets required jar files per the specification in the project object model ("pom") file which your Maven-ized project should have, and you don't have to keep track of jars on your own.
Look in your project for pom.xml, you might even post it here.
To check the sanity of the project and maven on your computer (ignoring Eclipse for a moment), be sure you have a good Internet connection, open a Terminal window, change directory ("cd") over to the project directory, and type "mvn install".
This should trigger the usual Maven build cycle. One of the first things it should do is download all the jars as identified in the pom.xml and store them deeply nested within your /Users/linus/.m2/ directory. Then it should compile all your java classes. Eventually it will probably build a jar file and copy it to somewhere.
If all this works, visit http://eclipse.org/m2e/ for directions to add the Maven plugin (m2e) to your Eclipse. Then your Eclipse should understand how to download jars and build the project per the pom.xml file.
Eventually this all should fix the original "Project is missing required library" problem.

Eclipse IDE behaves weirdly

I am getting a class not found exception for my Login Controller when I try to login to my application(It is a spring MVC Application). Before running the maven clean command the Application was running perfectly, I used maven clean and then again done a build using maven package command, suddenly it stopped working. I tried restarting the TomCat, re-deployed the application on TomCat, restarted the Eclipse IDE but nothing has worked so far.
Why this happens and what is the possible solution to this ??
Are you trying to run your project under Eclipse?
And does it generate source files?
If these are both true, you probably need to update the project config (select project or pom, right-click, Maven..Update..Project Configuration and Maven..Update..Project Dependencies).
The problem is that when Maven does a clean, it gets rid of the generated directories, which Eclipse observes and removes from your Build Path. But then when Maven rebuilds the directories, Eclipse doesn't notice it - you have to give it a hint.
Check your project for build errors and build path errors.
Make sure you've defined an M2_REPO classpath variable in Eclipse (Preferences - Java - Build Path - Classpath Variables).
If you're using the m2eclipe plugin, right-click your project and click on Maven - Update project configuration. That will configure an Eclipse build path for your project based on your pom dependencies.
If you're not using m2eclipse, execute mvn eclipse:clean eclipse:eclipse in your project folder. Then refresh your project in Eclipse.
What you haven't said is that you've checked and the missing class is in fact present.
A quick way to check is in the code editor SHIFT+CTRL+T and type the name of the missing class. Eclipse should not only show you the class if it exists, on highlighting the class, Eclipse should tell you which jar it is in.
Based on the info you have provided, I would say some dependecy in your POM has been removed by mistake or you need a newer version of some jar. If you find the class is not present, then you can figure out which jar you need by googling something like Maven 2 MyMissingClass jar that usually works for me.
Delete the .metadata folder in your eclipse workspace and then again open the workspace and import the required project.
This will solve your problem.
In .metadata folder it maintains the temporary copy of the project.
what worked in my case was simply removing the project from work space and importing it back again.
But I still don't know why it worked Vs why other things mentioned did not work (I tried updating dependencies in fact I removed all the dependencies from pom.xml and added everything again)

Source folders for a maven project in eclipse

I have a that uses maven... and I want to put it in my working environment with eclipse(Galileo)... the project is in a svn server, and I can create check out the project and everything looks OK. I even can run the unit test and everything is working there.
However, now that everything is there I wanted to work in the code, and oh surprise there are no packages in my project... I mean all the source code is in the src folder and browsing through it i can see all my files, ut if I open the files from there, the files are opened as text files with no coloring, but worst no help at all about errors in compilation.
I don't know what im I doing wrong now, because I had the same project in other machine and it was working well.
So here is what I did, please let me know if you notice if I did something wrong, miss any steps or anything that can help me:
In the SVN Repository (Using subclipse 1.6.10) I added my SVN Repository
Browsed to the folder where I have the pom file
Right Click> Check out as a Maven project...(Using m2eclipse 0.10.020100209)
Used the default options and finish.
The projects were created with no problem. I said projects because this maven project has modules, and each module became a project in eclipse.
Back in the java perspective, Right click in the project, Run as > maven test(Using JWebUnitTest, because I am testing a servlet)
BUILD SUCCESS!!
But as I said there is not packages so I can't really develop in this environment.
Any help??
Thanks!
When performing a svn checkout as maven project, m2eclipse writes what it does in a console window, accessible using the "console" panel, where there is a drop-down list of all available consoles. What does this console tells ?
Besides, if you can perform a maven test, you can also, in the maven menu (of your contextual menu) do a "update project configuration" which should do the same thing and configure eclipse for your project.
As a last test, is there a maven icon over your project ?
Have you tested your setup on command line with Maven ? Does a mvn package work without errors ? The location for the sources in Maven is src/main/java + Package name. Have you opened the project as a Maven project?
Thanks for your help, however I noticed that eclipse itself was behaving weird when I try to create a package there and I didn't have the option.
So I found out that for some reason that eclipse installation wasn't giving me any of the Java basic options.
So I download a new eclipse installation.. and I repeat the whole process and now is working...
No clue what was the problem with this eclipse because I was using it for other projects and I don't know when I lost the basic functionalities!
Thanks anyway!

Categories