I added dependency to my pom file. I see that it was added to the dependency tree. In the class Idea proposes to import the class I need but when I press alt+enter nothing happens, it just proposes to import again.
I tried all the solutions mentioned here: Intellij Cannot resolve symbol on import
(invalidate cache, maven reimport, checked source and dependency folders, everything). Nothing helped.
I use Intellij Idea 2019.2 Ultimate edition.
Related
I'm trying to learn how to build "Spring Web" web-applications using IntelliJ IDEA, and I'm encountering a very frustrating issue when I try to create a new project using "Spring Initializr".
For some reason when I choose to create a "Gradle Project", Java or Kotlin, I'm not able to import org.slf4j.LoggerFactory
If I manually add the import statement, it doesn't resolve, and if I try to get InteliJ to automatically add the import, it instead adds the following...
import com.sun.org.slf4j.internal.LoggerFactory
This satisfies the editor, but it won't compile. It errors with...
Symbol is declared in module 'java.xml.crypto' which does not export package 'com.sun.org.slf4j.internal'
If instead I create a "Maven Project", either Kotlin or Java, then it works as expected.
I'm keeping the other settings in spring initializr set to default values, Spring Boot=2.4.2, Java=11
IntelliJ IDEA says there are no updates available.
I've made various attempts to manual add the slf4j dependencies to build.gradle (for Java) or build.gradle.kts (for Kotlin), and nothing I've done has made any difference.
I've also tried various different options for Spring Boot and Java version, and again it has made no difference.
Update...
I've confirmed that it only happens on one of my computers, my primary development desktop computer. My laptop is working fine.
I can't find anything that's different between the 2 IDEA installations.
Things I've tried so far to fix it...
Invalidate Caches / Restart
Manage IDE Settings -> Restore Default Settings
Removing and re-adding JDK's
Uninstalling and re-installing IntelliJ IDEA
I've tried everything I can think of to fix this, but the problem persists.
Basically I can't use logging in any new Gradle Spring projects. It just won't load the org.slf4j library.
Update 2...
The nature of the problem isn't exactly what I thought it was initially. It turns out the code compiles and runs just fine with the correct import, but the IDE is reporting it as unresolved.
So it seems it's just an issue with the IDE's intellisense. This seems less serious, but it's still quite inconvenient having the editor incorrectly showing errors.
After identifying that the problem was just with the IDE editor intellisense, rather than with the code dependencies or project, I was eventually able to resolve it by closing the IDE, and then deleting the system folder "C:\Users<user>\AppData\Local\JetBrains\IntelliJIdea2020.3"
After restarting IntelliJ IDEA, the folder was re-created automatically and the issue resolved itself.
Hey I have created a Maven Project in IntelliJ and added some dependencies in my pom.xml for using external libraries. But I always have to import the classes in the class where I want to work with the classes of these libraries.
For example one dependency:
<dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-local</artifactId>
<version>${dl4j.version}</version>
</dependency>
And I use this line of code in my class:
DataAnalysis analysis = AnalyzeLocal.analyze(schema, recordReader);
HtmlAnalysis.createHtmlAnalysisFile(analysis, new File("X:/Churn_Modelling/analysis.html"));
I must import DataAnalysis.
I mean it would be ok if I would import the right class by clicking on import, but it is the wrong one.
You must use imports in your Java source code. Maven dependencies do not replace imports.
They make imports possible, though. Without the dependency, the import will fail.
If you can import the class then it means the dependency is successfully added to class path whether it is right or wrong.
Then the question is, is there only one DataAnalysis class in the class path. You can check by ctrl+space.
If it is the only one, then I think it is dependency problem. Your dependency version is dynamic,
${dl4j.version}
So in the build time may be it has different value. You can check it, IntelliJ maven tool Dependency part. In different dependency version, the class path would be changed.
If you are sure about, downloaded dependency with version is what you need, then it but the class is not what you expect, then try to restart the IntelliJ with clearing caches mode.
Hope one of these helps you.
I'm using intellij community 2018.2 and gradle 4.9 with kotlin 1.2.60
I've just downloaded a springboot2 pack from start.spring.io with kotlin/gradle support + web + h2 + jpa. I am following this official tutorial from spring.
gradle build and gradle test both work fine in command line.
In intellij though, I get the following error from the IDE:
Error:(9, 13) Kotlin: Type mismatch: inferred type is KClass<SpringExtension> but Class was expected
package org.ninrod.blog.blog
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit.jupiter.SpringExtension
#ExtendWith(SpringExtension::class) <-- RIGHT HERE
#SpringBootTest
class BlogApplicationTests {
#Test
fun contextLoads() {
}
}
here is the full source code of the sample: link.
How would one fix this in intellij so that the project compiles cleanly as in command line?
ps: here is a screenshot of the problem in intellij:
well, I uninstalled absolutely everything regarding java and then I installed everything from zero, removed all configurations, everything. Then it worked.
What you have is correct. I could not reproduce the error as shown in the screenshot below. And I was able to run your test without issue. So it is a false error. You can verify this by confirming the project builds via Gradle either from within IDEA of from the command prompt.
Try the following:
From the Gradle tool window, click the "Refresh all Gradle projects" button
From what I am seeing, and from my experience, I don't think think this will resolve the issue. But it is a quick hit and worth a wuick try.
It's possible the IntelliJ IDEA caches are corrupted. Go to *File > Invalidate Caches / Restart *. In the dialog window, click "Invalidate and Restart". After IDEA restarts and the project opens, wait for it to finish reindexing the project (watch the progress at the bottom). Once the reindex is done, see if the issue is resolved.
I suspect this will resolve the issue.
If the above does not work, then it's possible something is not configured correctly. Try recreating the IDEA project. Close the project, delete (or archive) the .idea directory and any *.iml files. Then import the project by selecting the build.gradle file. If you are not familiar with this, watch the "Working with Gradle in IntelliJ IDEA" video on the IntelliJ IDEA documentation page
Lastly, make sure you are running the latest version of IntelliJ IDEA (v2018.2), that was released two weeks ago. It is what I'm running. You using a lot of the latest and greatest tech (JUnit 5, Kotlin, Spring boot 2.x with Spring 5.x), so an older version of IDEA may have a problems. Finally, even though what you have worked for me, I'd recommend upgrading to the latest version of Kotlin (v1.2.60) in your gradle.properties
I do not get the error and can run the test:
I know this is trivial, but I bet I am not the only developer being annoyed by this issue:
I have a maven project building a Spring Hibernate frameowrk for a webapp.
I am building using Eclipse.
I use Eclipse shortcuts to import dependencies and often do so almost subconsciously.
Somewhere in my project dependencies I have a jar that includes the following package.
edu.emory.mathcs.backport.java.util
I often accidentally import this package instead of java.util and get compile or runtime errors.
QUESTION: How can I find out which dependency includes this package so I can (hopefully) exclude it using the Maven enforcer plugin.
This does not directly answer your question but may help you prevent the accidental import in the first place:
In Eclipse you can add a type filter in Preferences → Java → Appearance → Type Filters.
From the description there:
All types whose fully qualified name matches the selected filter strings will not be shown in the 'Open Type' dialog. They will also be ignored in content assist or quick fix proposals and when organizing imports. For example 'java.awt.*' will hide all types from the awt packages.
You can run mvn dependency:tree from the command line. It will show a tree of your dependencies and their transitive dependencies.
Within Eclipse you can open your pom.xml using the Maven POM Editor (default editor for POM files) and click on Dependency Hierarchy.
Within this view you can use a filter or select the dependency on the right side and the dependency tree for this dependency will be shown on the left side.
I recently encountered this issue with a Maven project in IntelliJ IDEA.
You can view transitive dependencies using the Maven command mvn dependency:tree to figure out where this package is creeping in.
However, it's worthwhile to mention that this problem can occur for Gradle or Maven projects. If you are using IntelliJ, then another workaround that will work for all of your projects - regardless of build tool - is to exclude the package altogether from the IDE's editor settings.
The steps to exclude the edu.emory.mathcs.backport.java.util package when using auto-import for all of your projects opened in IntelliJ are as follows (this is valid for IntelliJ IDEA 2022.2):
Ctrl + Alt + S (or click on File, then Settings)
Editor > General > Auto Import
Under the exclusion section, add the following exclusion with an IDE scope: edu.emory.mathcs.backport.java.util.*
Reference Screenshot
I have imported a library (JBox2D) using Maven in IntelliJ 13, for use in an android project. Maven didn't give me any error messages, so I assume the library was imported correctly.
However, as soon as I try use a class from the library, I get "Cannot resolve symbol". IntelliJ doesn't offer the option of adding an import statement, as it normally does.
How should I proceed?
Open the maven window and hit the reimport button (it's usually the first one on the toolbar). This will force IntelliJ to reimport your project based on changes in your pom.
Also, check the maven settings within IntelliJ and confirm that you're configured to use the same maven in IntelliJ as you're using on the command line.
If this doesn't work, then try to explain in more detail what you mean by your having imported a library with maven?