"Class already exists" error in IntelliJ on Groovy class - intellij-idea

in IntelliJ (2016.2 and previous) we have our Groovy classes marked red with the error "class already exists".
I think we can exclude that the cause is the stub-generation, as this is deactivated.
Probably it's caused in our constellation: We have included our compiled groovy (and java) classes in a jar that is registered as dependency.
Dependency MyProduct.jar contains com.mycompany.MyGroovyClass
Our source contains com.mycompany.MyGroovyClass
The error disappears if the dependency is registered with Test-Scope, in all other scopes the error appears.
However, in our structure we kinda have to include the compiled classes in a compile scope, as we want to avoid that each developer needs to compile all classes (I know about the compile in background ability, but we have a constellation that prevents this from working).
We have no errors in com.mycompany.MyJavaClass which exists as well in source and in MyProduct.jar.
Any ideas on how we can solve this?

We've been suffering the same issue, it seems to be that IntelliJ registers the Java class, but also the Groovy class, and because of that it is showing that message (BTW, we are using a Maven Project).
So we ended up by going to the target folder -> right click -> Mark Directory As -> Excluded. Then, this setting will be saved on the IML file, and it won't happen again.
Hope it works for your as well!
Cheers

I'm using gmavenplus-plugin:1.5
After marking target/generated-sources/generated-sources/main as "Excluded", The error disappeared. I even did "invalidate cache and restart", It persists the setting. This is great. Intellij 2017.1.5

We have two ways to fix this issue
Exclude Stub Directory
target folder -> generated-sources -> groovy-stubs -> Right click main folder -> Mark Directory As -> Excluded
Remove generateStubs goal from gmaven plugin
Remove <goal>generateStubs</goal> from gmavenplus plugin

Make sure you Mark the src folder as Sources Root and do the same for the test folder
Then delete the target folder (most likely it's marked in yellow) and don't worry it won't delete any code from your project
If the issue persist, proceed to go to File -> Invalidate Cache/Restart

Related

creating a Minecraft PVP client: error message when running minecraft [duplicate]

What are the possible causes of a "java.lang.Error: Unresolved compilation problem"?
Additional information:
I have seen this after copying a set of updated JAR files from a build on top of the existing JARs and restarting the application. The JARs are built using a Maven build process.
I would expect to see LinkageErrors or ClassNotFound errors if interfaces changed. The above error hints at some lower level problem.
A clean rebuild and redeployment fixed the problem. Could this error indicate a corrupted JAR?
(rewritten 2015-07-28)
Summary: Eclipse had compiled some or all of the classes, and its compiler is more tolerant of errors.
Long explanation:
The default behavior of Eclipse when compiling code with errors in it, is to generate byte code throwing the exception you see, allowing the program to be run. This is possible as Eclipse uses its own built-in compiler, instead of javac from the JDK which Apache Maven uses, and which fails the compilation completely for errors. If you use Eclipse on a Maven project which you are also working with using the command line mvn command, this may happen.
The cure is to fix the errors and recompile, before running again.
The setting is marked with a red box in this screendump:
try to clean the eclipse project
you just try to clean maven by command
mvn clean
and after that following command
mvn eclipse:clean eclipse:eclipse
and rebuild your project....
Your compiled classes may need to be recompiled from the source with the new jars.
Try running "mvn clean" and then rebuild
The major part is correctly answered by Thorbjørn Ravn Andersen.
This answer tries to shed light on the remaining question: how could the class file with errors end up in the jar?
Each build (Maven & javac or Eclipse) signals in its specific way when it hits a compile error, and will refuse to create a Jar file from it (or at least prominently alert you). The most likely cause for silently getting class files with errors into a jar is by concurrent operation of Maven and Eclipse.
If you have Eclipse open while running a mvn build, you should disable Project > Build Automatically until mvn completes.
EDIT:
Let's try to split the riddle into three parts:
(1) What is the meaning of "java.lang.Error: Unresolved compilation
problem"
This has been explained by Thorbjørn Ravn Andersen. There is no doubt that Eclipse found an error at compile time.
(2) How can an eclipse-compiled class file end up in jar file created
by maven (assuming maven is not configured to used ecj for
compilation)?
This could happen either by invoking Maven with no or incomplete cleaning. Or, an automatic Eclipse build could react to changes in the filesystem (done by Maven) and re-compile a class, before Maven proceeds to collect class files into the jar (this is what I meant by "concurrent operation" in my original answer).
(3) How come there is a compile error, but mvn clean succeeds?
Again several possibilities: (a) compilers don't agree whether or not the source code is legal, or (b) Eclipse compiles with broken settings like incomplete classpath, wrong Java compliance etc. Either way a sequence of refresh and clean build in Eclipse should surface the problem.
I had this error when I used a launch configuration that had an invalid classpath. In my case, I had a project that initially used Maven and thus a launch configuration had a Maven classpath element in it. I had later changed the project to use Gradle and removed the Maven classpath from the project's classpath, but the launch configuration still used it. I got this error trying to run it. Cleaning and rebuilding the project did not resolve this error. Instead, edit the launch configuration, remove the project classpath element, then add the project back to the User Entries in the classpath.
I got this error multiple times and struggled to work out. Finally, I removed the run configuration and re-added the default entries. It worked beautifully.
Just try to include package name in eclipse in case if you forgot it
Import all packages before using it, EX: import java.util.Scanner before using Scanner class.
These improvements might work and it will not give Java: Unresolved compilation problem anymore.
Also make sure to check compiler compliance level and selected jdk version is same
As a weird case, I encountered such an exception where the exception message (unresolved compilation bla bla) was hardcoded inside of generated class' itself. Decompiling the class revealed this.
I had the same issue using the visual studio Code. The root cause was backup java file was left in the same directory.
Removed the backup java file
When the build failed, selected the Fix it, it cleaned up the cache and restarted the workSpace.

Unable to run Kotlin scratch file

In a newly installed IntelliJ IDEA, I created a new Kotlin scratch file and tried to run it, but it gives me an error:
add kotlin script runtime jar to classpath
There is related issue on YouTrack: https://youtrack.jetbrains.com/issue/KT-33010
As a workaround you could add kotlin runtime manually:
Open "Project Structure | Modules"
Select your module, click on plus and add "Kotlin" facet
See "kotlin library not found ..." message and click on "Fix..."
Select your module in editor ( "Use casspath of module").
Just add one line of dependency
dependencies {...implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.5.21")...}
Not sure if there's any magical trick that sorted this out, but I simply created a new Scratch file and everything worked!
PS: In my case, I suspect the problem was related to me renaming the scratch file previously. Again, not sure! :)

Classes calling methods from other classes

I have got a several different classes under the same package in intellij idea,the problem is classes cannot recognize the other class' methods and throws cannot resolve symbol, have tried invalidating cache and restarting to no avail.
The question is prety unclear but I will try to guess what the problem is.
Have you marked the root folder of your project as sources root folder? This should help intellij to recognize the classes as one project. It should be done from:
right click over the folder -> mark directory as -> sources root.
When done the folder icon will be colored in blue and intellij should recognize the module(project) structure

How to mark a directory as a generated-test-source root in IntelliJ?

I'm trying out Dagger2 in Intellij 2016.1 (but not with gradle) on ubuntu.
Intellij creates dagger's generated sources in either
./out/production/<ProjectModule>/generated/ or
./out/test/<ProjectModule>/generated_tests/ depending on if it was generated from a source or test directory, respectively.
But from what I tell, I can only mark those directories as either sources root, test sources root, or generated sources root; there is no option for generated test sources root, say.
Why is this important? Because the generated test sources depend on my test sources. If they are marked as a generated sources root then Intellij cannot find the dependencies.
Note: I don't think they should be marked as test sources root because then Intellij tries to compile those again; unless there is some way of preventing this of which I am unaware.
So is there a way to mark this directory as a generated test sources root or something equivalent?
To mark a diectory as "generated test sources root", open the "Project Structure" dialog at Project Settings > Modules and click on the little "P" next to your folder of choice, and select the "For generated resources" button.
Dagger uses annotation processing to generate sources during compilation. IntelliJ has a specific configuration for this feature in Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors
When it is enabled IntelliJ automatically adds generated sources to project.
With annotation processing enabled I can see that generated test sources are marked both as Test Sources Root and Generated Sources Root. But when I try to manually set both flags it does not work - I get flags Sources Root and Generated Sources Root.
For me it looks like a bug.
Here's what worked for me. Create a directory in the Module root called generated and under it have two simlinks to <ProjectRoot>/out/production/<ProjectModule>/generated/ and <ProjectRoot>/out/test/<ProjectModule>/generated_tests/. Mark the first as Resource Root and the second as Test Resource Root.
I created the new directory and simlinks because it appears Intillij auto-marks <ProjectRoot>/out as Excluded.
I marked the directory as Test Resource Root so that Intellij doesn't try to compile the source twice to the same class. (Hint: big complains from the compiler.)
In the end, no red squiggles and auto-complete works.
Note: I didn't change the Intellij's generated sources directory for the module. (Well, I did to try another answer, but changed it back.)

IntelliJ IDEA Report Highlighting error when using routes in Controller

I have a Scala Play project. I'm using Play 2.2.1. I downloaded Scala, Play 2 supported and SBT plugins. Everything is OK, but When I call route on Action in the Controller appear following error(Look screenshots):
I'm using IntelliJ IDEA 12.1.6 Ultimate version. Scala version 2.10.2
Anybody know how to fix this problem?
Thanks in advance!
Edit
When I generate my project to Intellij IDEA via "play idea" command in play console, and I opened project in IDEA project structure was such:
Then I saw answer #millhouse and discussing on this githup([Play 2.2] "play idea" creates not working source mapping in target) and I removed following folders from Sources Folders:
target\scala-2.10\src_managed\main\controllers
target\scala-2.10\src_managed\main\views
and "Report highlighting error" disappeared, but now there is another error:
and
my route:
And I've changed "Sources Folders" as shown below:
But it doesn't help me. And remains "Unspecified value parametrs" error.
millhouse's answer helped me find the right answer. Play 2.2 (and perhaps earlier versions, haven't checked) output a scala version of the routes file to project_dir/target/scala-2.10/src_managed/main So for IntelliJ to get the highlighting right you need to make sure that src_managed/main is added as a source folder. The reverse routes for javascript are for some reason included in project_dir/target/scala-2.10/classes_managed so you'll have to add that as well. (In your screenshot it looks like classes_managed is set to excluded so you'll want to un-exclude it by pressing the x button on the right hand side.)
Here's a screen shot of my project structure:
Update:
In Play 2.3 (or perhaps recent versions of IntelliJ) adding the classes_managed no longer fixes the problem. Instead of adding classes_managed to the project's sources add it to it's dependencies. By doing that IntelliJ should be able to pick up the compiled routes.class.
This is a common problem, it's because IntelliJ needs to be shown where to look for the compiled Scala templates and routes file. Here's how I fixed it on my IntelliJ 12.1 installation:
Go to the Module Settings for your project in IntelliJ (select the top-level, hit F4)
Choose the Modules item in the Project Structure window that appears
Select the projectname item (as opposed to the projectname-build item)
Add the compiler's output directory to the Source Folders items;
For Scala 2.10 it's target/scala-2.10/classes
I personally find the IntelliJ UI for this to be very non-intuitive; you might need to take the target directory and/or one of its subdirectories out of the Excluded Folders first
After applying that change, IntelliJ should see the results of compiling each routes and .scala.html file as just-another source file, so they won't show as "broken" and auto-complete should work for them.