Not able to import okhttp project in intellij using maven plugin - java

I am not able to successfully import the okhttp project in the intellij.
https://github.com/square/okhttp
I am using intellij community edition: 2016.1
Maven: 3.0
When I hit make. It shows following error message for "okhttp-tests" module:
Error:(94, 49) java: diamond operator is not supported in -source 1.5
(use -source 7 or higher to enable diamond operator)
The language level for all the submodules is automatically set to 5, while for parent it is set to 7.
When I change the language level for okhttp-tests module to 7 and hot make, ideas shown me following error message:
Error:java: javacTask: source release 1.7 requires target release 1.7
Am i using wrong version of idea/maven ?
Please help.
Also there is no detailed documentation available for importing project/ setting up dev environment on the git repo.
I would like to request okhttp members to create a descriptive Contributors.md file for beginners like me.

If JAVA_HOME is set correctly, Please also check the Project Parent POM file for the compiler Version of maven-compiler-plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>

Related

Why javac target option can not be lower than source option?

I have read about source and target options for javac that they define a version that my source code requires to compile and oldest JRE version i want to support, respectively. While using Maven build tool i have defined these parameters in pom.xml like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>15</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
, but target version can not be set lower than source version, why is it so?
Trying to compile with this configuration results in error: "Fatal error compiling: warning: source release 15 requires target release 15". Same with any other combination of source version being higher than target.
Because that is how it is designed. From the documentation of javac in Java SE 15 for the --target option:
Note: The target release must be equal to or higher than the source release. (See --source.)
I guess the usual is to have old source code that you want to run in newer releases and, when starting a project, you choose your source release as the oldest release that you want to support.
Look at this answer for a discussion about backward and forward compatibility:
In brief, we can say:
JDK's are (usually) forward compatible.
JRE's are (usually) backward compatible.
You can think about it as this: JDKs can compile (usually) for newer releases and JREs can run (usually) older releases.

intellij idea internal java compiler error

I am running a simple pattern matcher program exactly as in
test_harness
Information:java: Errors occurred while compiling module 'demo_java8'
Information:javac 1.8.0_121 was used to compile java sources
Information:2018-02-06 10:15 - Compilation completed with 1 error and 0 warnings in 376ms
Error:java: Compilation failed: internal java compiler error
However terminal command javac xxx.java and java xxx run properly.
Running the first hello world program gives the same error.
In my case, the issue is resolved by adding a type parameter object to a class instance that needed it. In the following code replaced new ParameterizedTypeReference<> with new ParameterizedTypeReference<Map<String, Object>>
return getRestTemplate().exchange(uri,
HttpMethod.POST,
new HttpEntity<>(headers),
new ParameterizedTypeReference<Map<String, Object>>() {
});
It turned out that "odd" error description is because of maven plugin was missing. And it's default was 1.5 while Console class is only available since 1.6. Instead of jar or class cannot be found, it gives a blur description internal java compiler error. Information:javac 1.8.0_121 was used to compile java sources was a hint that the javac version and sdk version mismatch. Besides, I was surprised that intellij idea run function uses maven build (I thought it was just for cli) .
If I have to guess, your IntelliJ is using the JDK packed with IntelliJ, try to set up your JDK for your project, in Project Structure make sure the JDK match your Java environment.
JDK selection
If you use a maven project and for example, you use java version as 1.8 please use this plugin where you specify which version you are using.
Working example:
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Maven facet version issue in web application

I am trying to create a maven web app, which will be part of my maven ear application. I tried creating web app from command line as well as from option within eclipse but both ways giving me error like "Dynamic Web Module 3.1 requires Java 1.7 or newer." I tried changing build path JDK to Java 1.8 and compiler level to 1.8 and saved but this didn't resolve the issue. When I do right click on project "maven update", the JDK again getting reset to old one not sure why it's not saving my changes. Is it issue with eclipse(I tried both LUNA and MARS version of eclipse)? How to resolve this issue as it's not letting me run my application from eclipse.
Also I am getting this access restriction warning "Access restriction: The type 'XmlElement' is not API (restriction on required library 'C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar')". If I remove JDK from build path and add it again it disappears. But when maven update is done error reappears. Please someone suggest me how to resolve these issues ?
Add following in pom.xml and do maven update
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

How do I update the Maven configuration in Eclipse Luna?

I am using Java 8 for my JRE in Eclipse Luna with with the m2e Maven plugin. I see this warning as soon as I create a Maven project: Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace that are strictly compatible with this environment. I see lots of older advice on this site on how to deal with this error, but nothing related to Java 8. I also that Oracle says J2SE was deprecated in 2008. The most promising answer I have found thus far comes from #Pascal Thivent here:
Warning - Build path specifies execution environment J2SE-1.4
But these instructions no longer seem to be relevant for Luna:
Eclipse project Properties > Maven > Update Project Configuration
I only see "Lifecycle mapping" under Maven.
Can someone please explain (1) what this error means (2) if it is still a worry, given that J2SE-1.5 seems to have been deprecated (3) how to best fix it in Luna?
This error is because the project pom file is instructing maven to compile against java 1.5.
It is best to compile and test to a current java version. In your case java 8.
I am unsure how to fix it inside the eclipse IDE, but the project pom file can be edited to include the compile version as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Resource specification not allowed here for source level below 1.7

While running java program getting following error in eclipse
Caused by: java.lang.Error: Unresolved compilation problem:
Resource specification not allowed here for source level below 1.7
Although I am using java 1.7.25 and all eclipse settings are in place but not sure why getting this error
SOLUTION.
Problem solved by updating project in eclipse using maven.
Despite of the fact that you are using Java 1.7 you can compile source code as if you had compiler from Java 1.6 (this can be useful for example for cross-compilation).
As Shivam Tiwari said in Eclipse you can change it in window->preferences->java->Compiler Compiler
If you are using Maven you should add the following plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
In Eclipse. Go to Project->Properties->Click on Java Compiler and change Compiler compliance level to 1.8 or above.
It works for me many times.
Maven update with 'Force snapshot release' did the trick. pom.xml doesn't need to be changed.

Categories