IntelliJ IDE error java: warnings found and -Werror specified - intellij-idea

I am getting below error in IDE (2020.3.4) in my java project when I tried to build/Run the project. I am not be able to build/run the application via IDE because of this issue.
java: warnings found and -Werror specified
I got below line in my POM file and tried to comment that as well. But same error.
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<compilerArgs>
<!-- <arg>-Werror</arg>-->
<arg>-verbose</arg>
</compilerArgs>
<source>11</source>
<target>11</target> <!--or <release>10</release>-->
</configuration>
Is this any setting specific error.

I was facing the same issue, which I fixed by removing Werror flag from Intellij:
file > settings > Build, Execution Deployment > Java Compiler
At the bottom, you can find it under Override compiler parameter from there you can remove the option by clicking on - button
Also, verify if you are passing this flag to the maven-compiler-plugin and remove it if it is not needed.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>-Werror</arg>
<arg>-Xlint:all</arg>
<arg>-Xlint:-deprecation</arg>
<arg>-Xlint:-options</arg>
<arg>-Xlint:-processing</arg>
<arg>-Xlint:-serial</arg>
</compilerArgs>
<showWarnings>true</showWarnings>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

I got below line in my POM file and tried to comment that as well. But same error.
Make sure you have removed this flag from all possible places in pom.xml files and invoked Reload action in the Maven tool window.

I was searching for -Werror and was getting no results back.
In a different form it can be without - as a tag in maven-compiler-plugin, example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments combine.children="append">
<Werror />
</compilerArguments>
</configuration>
</plugin>
Commenting out this piece helped.

Related

"No public or protected classes found to document" error from path with accents

My Maven Java 8 project is inside a path which contains accents: C:\Développements\myproject.
When I use maven-javadoc-plugin (event with last 2.10.4 version) I have this error when I try to generate the javadoc of my project (from IntelliJ IDEA 2016.2.4):
[ERROR] javadoc: warning - No source files for package com.mycompany.myproject
[ERROR] javadoc: error - No public or protected classes found to document.
This is strange because I have documented classes in this project.
This error can also occur if you have no public methods in your test classes, which is exactly what can happen because Sonar lint rule S5786 says JUnits should have default package visibility, for readability. Fortunately, you can use the -package javadoc option, to fix this. If you put this in your parent pom:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
<junit.version>5.7.0</junit.version>
</properties>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<source>8</source>
<additionalOptions>-package</additionalOptions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<distributionManagement>
<site>
<id>yourid</id>
<url>file:///var/www/html/maven</url>
</site>
</distributionManagement>
then
mvn site-deploy
will give you your default maven site along with the javadoc. Included everything relevant for a Java 8 project.
Had this happen when I created a package-private class with a main method. After marking the class as public the packaging step worked again.
This is not a Maven or plugin problem but purely a Windows problem. Microsoft is too stupid to have a proper encoding set in cmd.exe. You have some stupid DOS encoding. Java's javadoc uses that to read the #options file and fails.
Set _JAVA_OPTIONS=-Dfile.encoding=UTF-8 and you are done. Alternatively, use a Linux distribution or FreeBSD.
The issue remains closed.
Actually this is a referenced bug from maven-javadoc-plugin project: MJAVADOC-333.
So since it is not fixed (it is currently "closed"...) one should just remove the accents from your project path...
Apart from the special character (accent) problem, this may be a problem with your pom.xml:
I had the same problems right now with a project created in Eclipse.
If you create a project in eclipse, it will put java packages/sources directly within the src folder and add the following line to your pom.xml:
<sourceDirectory>src</sourceDirectory>.
If you then decide to move your java files according to the maven conventions and forget to update or remove the sourceDirectory tag, you will end up with exactly the same error:
Your project will build fine, but javadoc will not find it`s sources...

Maven exclude directory from compiling and running under tests

I'm trying to set up my first maven project for my code which can be found here:
https://github.com/jkinable/jorlib
I have 3 pom files:
https://github.com/jkinable/jorlib/blob/master/pom.xml
https://github.com/jkinable/jorlib/blob/master/jorlib-core/pom.xml
https://github.com/jkinable/jorlib/blob/master/jorlib-demo/pom.xml
I would like to exclude all files in the following directory from both compilation and testing:
jorlib-core/src/test/java/org/jorlib/frameworks/columnGeneration/tsp
Note the "test" part. According to this website I can use the maven-compiler-plugin together with excludes and testExcludes for that. So I've added the plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/org/jorlib/frameworks/columnGeneration/tsp/**</exclude>
<exclude>**/org/jorlib/frameworks/columnGeneration/tsp/**/*</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/**/*</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/Master.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/TSPMasterData.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/ExactPricingProblemSolver.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/cuts/SubtourInequalityGenerator.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/cuts/SubtourInequalityGenerator.java</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/**/*</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/**</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/*</exclude>
</excludes>
<testExcludes>
<exclude>**/frameworks/**</exclude>
<exclude>**/org/jorlib/frameworks/columnGeneration/tsp/**</exclude>
<exclude>**/org/jorlib/frameworks/columnGeneration/tsp/**/*</exclude>
<exclude>/org/jorlib/frameworks/**</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/*</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/**/*</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/Master.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/TSPMasterData.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/ExactPricingProblemSolver.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/cuts/SubtourInequalityGenerator.java</exclude>
<exclude>**/src/test/java/org/jorlib/frameworks/columnGeneration/tsp/cg/master/cuts/SubtourInequalityGenerator.java</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/**/*</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/**</exclude>
<exclude>**/src/**test**/java/org/jorlib/frameworks/columnGeneration/tsp/*</exclude>
</testExcludes>
</configuration>
I've tried a huge number of exclude rules (the above are just a small subset). I run "mvn clean; mvn test" but still I keep getting compile errors on classes within
jorlib-core/src/test/java/org/jorlib/frameworks/columnGeneration/tsp
Any suggestions on how to deal with this?
The solution appears to be simple. The following code suffices (notice the sue of testExcludes):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testExcludes>
<exclude>**/org/jorlib/frameworks/columnGeneration/tsp/**/*</exclude>
</testExcludes>
</configuration>
</plugin>
This didn't work for me because somewhere else in my code I had a lonely import statement which imported a file from the directory I wanted to exclude. Albeit this particular file wasn't used at all (I just forgot to delete the import statement), the import statement alone caused maven to compile the excluded directory anyway. Makes me wonder why maven doesn't detect this 'clash'? Nevertheless I'm happy to report that my problem is solved.

IntelliJ: evaluate lambda expression raises an compile error in debugging

I'd like to evaluate code including lambda expression with Intellij 'Evaluate Code Fragment' feature. But then, Intellij raises an error Unable to compile for target level 1.8. Need to run IDEA on java version at least 1.8, currently running on 1.6.0_65-b14-462-11M4609
The evaluating code is very simple as below.
Set<Integer> set = new HashSet<>();
set.add(1);
set.stream().map(v->v).collect(Collectors.toSet());
My Intellij version is 14.0.3 and according to official document, version 14 supports lambda expression evaluation.
How can the feature be available?
Putting together the comments from adrian lange and Bohuslav Burghardt, either upgrade to the latest IntelliJ 14.1 which comes bundled with Java 1.8, or switch your current IntelliJ to use Java 1.8. See bug IDEA-132099.
To switch to Java 1.8:
Install Java 8
Change IntelliJ to use Java 8. See these instructions.
Even after applying above defined project specific settings on IntelliJ as well as Eclipse, it was still failing for me !
what worked for me was addition of maven plugin with source and target with 1.8 setting in POM XML:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.abc.sparkcore.JavaWordCount</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can switch runtime version of JDK:
Start the IDE, Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter.
Start the IDE, use Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter.
Switch IDE Boot JDK dialog appears. Select the version from the drop-down or click "…" entry to specify the custom location.
Unable to compile for target level 1.8...Currently running on version 1.6
My guess is that your project is being run on a version of Java that doesn't support lambdas. Make sure you have Java 8 installed, and also make sure to change your project level to use Java 8, like so:
Notice that it explicitly mentions lambdas

Error when running maven generated executable jar

I'm having (a strange) problem when executing a maven generated executable jar:
user#host$ java -server -jar MyJar.jar
Error
(and nothing more than this!!!)
Do you have any idea what this king of error comes from ?
In my pom.xml, I copy all the dependencies to a lib folder with:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${artifactId}-${version}/${artifactId}-${version}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
And then I generate a .jar including the classpath (+ a prefix pointing to the lib folder):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${project.build.directory}/${artifactId}-${version}/${artifactId}-${version}/bin</outputDirectory>
<finalName>MyJar</finalName>
<archive>
<manifest>
<mainClass>
com.company.package.Main
</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
The generated MANIFEST.MF seems to contain the proper classpath.
Thanks a lot for your help!
The error isn't saying much and this is indeed weird. Are you using Sun JDK?
Anyway, I don't really get how the dependencies get bundled into the final JAR with your setup and I don't think it contains everything required (I may be wrong of course).
Actually, I wouldn't even try to fix your current setup. To create an executable jar, you should prefer the assembly plugin. See this recent answer for example. Please modify your pom.xml with the suggested configuration (this will take 30 seconds) and try again. Then, please update your question with the new result/error, the pom.xml and the manifest.
I created a new Maven repository, rebuilt all the maven dependencies and somehow the issue was solved.
I've no idea how this happened, because I was just able to run without the jar ...
But thanks for your help anyway

JAVA_HOME gets mangled by Maven

I'm retrofitting bunch of existing Java projects with unified Maven build. Since each project is mature and has established Ant based build all I'm using maven-antrun-plugin to execute existing build.xml as follows:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="compile" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
When I run mvn compile build fails with this message:
[INFO] An Ant BuildException has occured: The following error occurred
while executing this line:
build.xml:175: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Java\jdk1.6.0_13\jre"
What puzzles me is
I have JAVA_HOME=C:\Java\jdk1.6.0_13 as part of my environment setup and when mvn.bat is executed that is exactly value I'm getting, however as you see in the error message it comes up as C:\Java\jdk1.6.0_13\jre
If I run ant compile everything compiles just fine
Does it mean that perhaps maven-antrun-plugin does something like set JAVA_HOME=%JAVA_HOME%\jre? I searched my batch/build files I can't find where that change occurs
Thats the down side of external links in an accepted answer. Codehaus shut down and thus the solution is gone. For reference here's the content behind the link - you basically only need to copy the <dependencies>...</dependencies> block to your antrun plugin...
The maven-antrun-plugin runs ant with JAVA_HOME set to the jre subdirectory of the JDK, even if the JAVA_HOME for the overall run is a JDK.
There is documentation elsewhere about how to create a dependency at the project level for the JDK's tools.jar, but this does not help out antrun, which is a plugin.
The following profile does the job. The '..' in the path hauls up past the 'jre' directory to the lib directory.
<profiles>
<profile>
<id>tools.jar</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
I was able to fix this by putting the following property definition in my ant build.xml file:
<property name="build.compiler" value="extJavac"/>