#Override is not allowed when implementing interface method - java

I have the problem mentioned in the title. You could say that this thread duplicates another one: How do I turn off error validation for annotations in IntelliJ IDEA?
But the solution given there doesn't work. They say that I need to take the following action:
In the Project Structure | Project dialog, change the Project language Level to 6.0 - #Override in interfaces.
However, the Project language Level is 6.0 at the moment, but I still see the error.
Vic, here is the window and there is no JVM version right under Language level (unfortunately I can't post images because I have 10 reputation)

If your project has multiple modules, also check that every module uses language level 6 or above, or use the project's language level (see Project Settings > Modules > xxx > Language level).
You may need to reload your project once it is modified.

At your module/project, Right click to see context menu:
Choose Open Module Settings or press F4. In setting windows:
Set value for Choose Language level section.
You also should check Project language level by this way: press Ctrl+Alt+Shift+S

A simpler solution - inline
Put the caret on the #Override word and move the caret on the left side until the red bulb icon will appear. Then click on it.
Click on Set language level to 6 - Override in interfaces
The method above is an alternative to the following approach:
Go to File > Project Structure... or press Ctrl+Alt+Shift+S
Go to Project Settings > Modules > Sources > Language level and choose any level that is 6 or greater than 6.

If you are using maven, add maven compiler plugin to the project's pom.xml file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
This solved the issue for me.

There's also a language level for every module. Please check your module settings in the Project Structure.

I ran into this problem for the first time while using a multi module maven project. As other answers / IDE suggested, we need to set the language level.
Rather than changing the setting of IDE, to make the project IDE agnostic, I update the parent pom with below properties, which solved the issue.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

In JIdea 2020.1.2 and above,
Go to Project Structure [ Ctrl+Alt+Shift+S
]
Select Modules sub section
Select each module
Under sources-section, check Language Level
Change the Language Level as required
NOTE:
If you get below error after this change,
Error:java: Compilation failed: internal java compiler error
You have to change the target bytecode version as well.
Go to Settings [ Ctrl+Alt+S
]
Select Java Compiler
Select module in the table
Change the byte-code version to map what you selected in the previous step for language-level

Related

Netbeans: Allow external build participant during Maven project loading (Maven Tiles)

In the process of migrating dozens of individual Maven projects to use Maven Tiles, I've hit a challenge in the Netbeans IDE 12.0 -- with all common definitions, like the maven-compiler-plugin including the desired JDK version (11), being centralised in reusable tiles.
The pom.xml looks like this:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.example</groupId>
<artifactId>failing-project</artifactId>
<version>1.0-SNAPSHOT</version>
<name>failing-project</name>
<build>
<plugins>
<plugin>
<groupId>io.repaint.maven</groupId>
<artifactId>tiles-maven-plugin</artifactId>
<version>2.23</version>
<extensions>true</extensions>
<configuration>
<tiles>
<tile>my.example.maven-tile:jar-tile:1.0</tile>
</tiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Running the mvn clean build command directly works fine, and starts out as expected with something like this:
--- tiles-maven-plugin: Injecting 3 tiles as intermediary parent artifacts for my.example:failing-project...
Mixed 'my.example:failing-project:1.0-SNAPSHOT' with tile 'my.example.maven-tile:jar-tile:1.0' as its new parent.
Mixed 'my.example.maven-tile:jar-tile:1.0' with tile 'my.example.maven-tile:build-tile:1.0' as its new parent.
Mixed 'my.example.maven-tile:build-tile:1.0' with tile 'my.example.maven-tile:nexus-tile:1.0' as its new parent.
Mixed 'my.example.maven-tile:nexus-tile:1.0' with original parent '(no parent)' as its new top level parent.
The rest of the build is then behaving as before, like all the common definitions are part of my project's pom.xml.
Unfortunately, Netbeans indicates "Project Problems". Selecting the "Resolve Project Problems" option, brings up a dialog with the following entry:
Custom build participant(s) found
The IDE will not execute any 3rd party extension code during Maven project loading.
These can have significant influence on performance of the Maven model (re)loading or interfere with IDE's own codebase. On the other hand the model loaded can be incomplete without their participation. In this project we have discovered the following external build participants:
io.repaint.maven.tiles.TilesMavenLifecycleParticipant
In the absence of an explicit maven-compiler-plugin configuration, Netbeans has to pick some JDK version and choses poorly (JDK 5). That results in all "newer" constructs (e.g. every single lambda expression) to be highlighted as compilation error, making the IDE pretty much unusable.
lambda expressions are not supported in -source 5
(use -source 8 or higher to enable lambda expressions)
Other IDEs do not appear to have such limitations, but I'd rather find a solution for Netbeans to not force all developers who are used to it, to change.
This challenge was already identified in the Maven Tiles project, but no solution was presented until it got closed with the comment "closing as no one cares about netbeans anymore.", which I disagree with.
https://github.com/repaint-io/maven-tiles/issues/10
Question is: Is there a way to do one of the following things?
Allow such an "external build participant" to be included in the Maven project loading.
Globally enforce a default maven-compiler-plugin <source> and <target> (and I don't mean the build environment via something like <netbeans.hint.jdkPlatform>JDK_11</netbeans.hint.jdkPlatform>).
Suppress the automatic Maven project loading and rather rely on the explicit "Build" action to be triggered, while still highlighting the actual errors in my code.
EDIT:
Via right-click on the project, one can go to "Properties" > "Sources" > "Source/Binary Format" and select "11" there, which solves the false negatives on the compilation errors but only by inserting the following properties into my pom.xml, which I wanted to avoid, since that may contradict my Tile definition and is unnecessary for other IDEs:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

JavaFX maven samples for CLI and IDE, what is the difference? [duplicate]

I use IntelliJ IDEA as my development environment, and Maven for dependency management. I frequently build my project structure (directories, poms, etc) outside of IDEA and then import the project into IDEA using Import project from external model. This works great, except that in my poms I specify that the maven-compiler-plugin should use JDK 1.6, and when I import, IDEA informs me that the Language Level Changed and that Language level changes will take effect on project reload, and then prompts to reload the project. This is annoying because I always use the same JDK version.
How do I change the default JDK that IntelliJ IDEA uses, so that I don't have to reload my project every time I import a new project?
This setting is changed in the "Structure for New Projects" dialog. Navigate to "File" -> "New Projects Setup" -> "Structure..."
Next, modify the "Project SDK" and "Project Language Level" as appropriate.
Previous versions of IntelliJ IDEA had this setting in "File" -> "Other Settings" -> "Default Project Structure...".
IntelliJ IDEA 12 had this setting in "Template Project Structure..." instead of "Default Project Structure..."
Download and unpack a JDK archive file (.tar.gz) and add it as a SDK in the 'Project Structure' dialog box ( Ctrl+Alt+Shift+S )
click on the gif to enlarge
Also make sure to set an appropriate 'Project language level'. I forgot to do that when creating the GIF.
Project Structure > Project > Project language level
For Java 8 set it to 8, for Java 9 set it to 9, and so on.
I am using IntelliJ IDEA 14.0.3, and I also have same question. Choose menu File \ Other Settings \ Default Project Structure...
Choose Project tab, section Project language level, choose level from dropdown list, this setting is default for all new project.
I have found out that in recent versions of IntelliJ IDEA requires Java 1.8 but is not configured by default.
We can change the path or configure from Project Settings > Project > Project SDK
here we can edit or add the JDK´s path.
(in my case the path is located in C:\Program Files\Java\jdk1.8.0_102)
Change JDK version to 1.8
Language level File -> project Structure -> Modules -> Sources -> Language level -> 8-Lambdas, type annotations etc.
Project SDk File -> project Structure -> Project 1.8
Java compiler File -> Settings -> Build, Executions, Deployment -> Compiler -> Java compiler
One other place worth checking: Look in the pom.xml for your project, if you are using Maven compiler plugin, at the source/target config and make sure it is the desired version of Java. I found that I had 1.7 in the following; I changed it to 1.8 and then everything compiled correctly in IntelliJ.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
The above responses were very useful, but after all settings, the project was running with the wrong version. Finally, I noticed that it can be also configured in the Dependencies window.
Idea 2018.1.3 File -> Project Structure -> Modules -> Sources and Dependencies.
To change the JDK version of the Intellij-IDE himself:
Start the IDE -> Help -> Find Action
than type:
Switch Boot JDK
or (depend on your version)
Switch IDE boot JDK
For latest version intellij, to set default jdk/sdk for new projects go to
Configure->Structure for New Projects -> Project Settings -> Project SDK
I am using IntelliJ 2020.3.1 and the File > Other Settings... menu option has disappeared. I went to Settings in the usual way and searched for "jdk". Under Build, Execution, Deployment > Build Tools > Maven > Importing I found the the setting that will solve my specific issue:
JDK for importer.
On my linux machine I use a script like this:
export IDEA_JDK=/opt/jdk14
/idea-IC/bin/idea.sh

IntelliJ imports project from POM using wrong JDK version

We have a Maven based Android build, and we just made the switch from JDK 6 to 7.
This came with its share of IntelliJ problems though. What happens is that every time it detects a change in the POM, and reimports/refreshes the project, it returns to selecting the old "Module SDK", the one that's configured to use Java 6:
Even if I manually delete these SDKs from the "Platform Settings" dialog, they keep reappearing as "Maven Android API 19 Platform (N)" where N is the number used to disambiguate it from all the other (identical) SDKs.
I should mention that we do specify in the POM that Java 7 is targeted. I tried to set both the compiler plugin language level, and the maven.compiler.* properties (not sure if that accomplishes the same thing or not), without luck:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
shouldn't IntelliJ pick that up and always configure the project to use a Java 7 SDK? Am I missing something?
I noticed that the problem disappears when I remove any references to 1.6 SDKs entirely in IntelliJ. Not surprising I guess, but also not viable since I have other projects that still rely on the presence of a Java 6 SDK.
I encountered a very similar issue with Maven projects I'd created using IntelliJ (version 14.x in my case). I'd configured IntelliJ to use JDK 8 in the Project Settings but the IDE continued to highlight issues in my code (e.g. complaining about the usage of #Override).
It turns out that the Maven Settings take precedence here, which in my case defaulted to JDK 1.5 (hence the IDE redlines). Changing the settings here does resolve the issue, but only temporarily because they revert back whenever the Maven projects are reimported, or when IntelliJ is restarted.
The permanent fix is to explicitly declared the JDK version in your Maven pom file, as explained in these items.
stop IntelliJ IDEA to switch java language level everytime the pom is reloaded (or change the default project language level) by #vikingsteve
IDEA: javac: source release 1.7 requires target release 1.7 by #bacchus
Here's what they've said you need to add to your pom.xml file.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
These settings get reflected in the Maven Settings in the IDE and resolved the issue for me.
It will pick up the jdk that you choose in project structure please change it there.
File > project structure > project setting > project > project sdk choose 1.7.
If 1.7 is not present go to
File > project structure > Platform setting > SDKs addd 1.7 there.
It's also important to note that you need to change the runner (jdk level) of your maven.
Maven > Runner > JRE

How do I change the IntelliJ IDEA default JDK?

I use IntelliJ IDEA as my development environment, and Maven for dependency management. I frequently build my project structure (directories, poms, etc) outside of IDEA and then import the project into IDEA using Import project from external model. This works great, except that in my poms I specify that the maven-compiler-plugin should use JDK 1.6, and when I import, IDEA informs me that the Language Level Changed and that Language level changes will take effect on project reload, and then prompts to reload the project. This is annoying because I always use the same JDK version.
How do I change the default JDK that IntelliJ IDEA uses, so that I don't have to reload my project every time I import a new project?
This setting is changed in the "Structure for New Projects" dialog. Navigate to "File" -> "New Projects Setup" -> "Structure..."
Next, modify the "Project SDK" and "Project Language Level" as appropriate.
Previous versions of IntelliJ IDEA had this setting in "File" -> "Other Settings" -> "Default Project Structure...".
IntelliJ IDEA 12 had this setting in "Template Project Structure..." instead of "Default Project Structure..."
Download and unpack a JDK archive file (.tar.gz) and add it as a SDK in the 'Project Structure' dialog box ( Ctrl+Alt+Shift+S )
click on the gif to enlarge
Also make sure to set an appropriate 'Project language level'. I forgot to do that when creating the GIF.
Project Structure > Project > Project language level
For Java 8 set it to 8, for Java 9 set it to 9, and so on.
I am using IntelliJ IDEA 14.0.3, and I also have same question. Choose menu File \ Other Settings \ Default Project Structure...
Choose Project tab, section Project language level, choose level from dropdown list, this setting is default for all new project.
I have found out that in recent versions of IntelliJ IDEA requires Java 1.8 but is not configured by default.
We can change the path or configure from Project Settings > Project > Project SDK
here we can edit or add the JDK´s path.
(in my case the path is located in C:\Program Files\Java\jdk1.8.0_102)
Change JDK version to 1.8
Language level File -> project Structure -> Modules -> Sources -> Language level -> 8-Lambdas, type annotations etc.
Project SDk File -> project Structure -> Project 1.8
Java compiler File -> Settings -> Build, Executions, Deployment -> Compiler -> Java compiler
One other place worth checking: Look in the pom.xml for your project, if you are using Maven compiler plugin, at the source/target config and make sure it is the desired version of Java. I found that I had 1.7 in the following; I changed it to 1.8 and then everything compiled correctly in IntelliJ.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
The above responses were very useful, but after all settings, the project was running with the wrong version. Finally, I noticed that it can be also configured in the Dependencies window.
Idea 2018.1.3 File -> Project Structure -> Modules -> Sources and Dependencies.
To change the JDK version of the Intellij-IDE himself:
Start the IDE -> Help -> Find Action
than type:
Switch Boot JDK
or (depend on your version)
Switch IDE boot JDK
For latest version intellij, to set default jdk/sdk for new projects go to
Configure->Structure for New Projects -> Project Settings -> Project SDK
I am using IntelliJ 2020.3.1 and the File > Other Settings... menu option has disappeared. I went to Settings in the usual way and searched for "jdk". Under Build, Execution, Deployment > Build Tools > Maven > Importing I found the the setting that will solve my specific issue:
JDK for importer.
On my linux machine I use a script like this:
export IDEA_JDK=/opt/jdk14
/idea-IC/bin/idea.sh

Maven m2e enforces its own compiler settings - disable

After starting Eclipse, Mven seems to set the compiler settings to 1.5 and forget all the other global code style settings to ensure a higher code quality.
Is there some way to disable this feature? Or can I specify all compiler and code style checks in my POM?
It is very annoying because Ecplise can't run the app because of not allowed override annotations for interfaces. The tick in Java compiler -> Enable project specific settings is always set after a restart.
You can set the compiler source and target (byte-code) versions in your pom.
See http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Code style checks can be configured in the pom as part of the maven reports, see http://maven.apache.org/plugins/maven-checkstyle-plugin/
but I'm not sure whether the integration will pick these up.
The simplest way is to add to your POM
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
See default maven compiler setting for another solutions.
If you don't want the m2e eclipse plugin actively messing with your project settings, use the maven-eclipse-plugin's eclipse goal to generate your eclipse settings.
It'll generate your eclipse settings based off of what you have in your pom, so you'll still need to set the maven compiler settings in your pom if you don't want to set them every time you regenerate your eclipse project files when you update your pom.
If you take a look at the detailed configuration for that plugin, there are instructions for how to generate various pieces of eclipse metadata.

Categories

Resources