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.
Related
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>
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>
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.
I have a maven project that is compiled for both Java 1.7 and for Java 1.6.
The 1.7 jar is the default artifact, but I don't know which how to identify the 1.6 jar.
I could add a suffix to the artifact id, to the version, or set a classifier.
Which is the preferred way? What happens when another project A depends on the 1.7 jar, project B depends on the 1.6 jar, and project C depends on A and B?
OK, so after some reading, I think classifiers are the way
From the maven docs
classifier:
The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.
As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 and the second one with jdk14 such that clients can choose which one to use.
Then to solve the issue of forcing one jar over an other, in project C's pom, use the <dependencyManagement> section and specify the classifier there.
Hope this helps
you have to declare in pom.xml file the java compiler version:
<build>
<finalName>skremm_mobile_web</finalName>
<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>
I have started a maven project trying to implement the MapReduce algorithm in java 1.5.0_14. I have chosen the 0.20.2 API hadoop version. In the pom.xml i'm using thus the following dependency:
< dependency>
< groupId>org.apache.hadoop< /groupId>
< artifactId>hadoop-core< /artifactId>
< version>0.20.2< /version>
< /dependency>
But when I'm using an import to the org.apache.hadoop classes, I get the following error:
bad class file: ${HOME_DIR}\repository\org\apache\hadoop\hadoop-core\0.20.2\hadoop-core-0.20.2.jar(org/apache/hadoop/fs/Path.class)
class file has wrong version 50.0, should be 49.0.
Does someone know how can I solve this issue.
Thanks.
Maven by default compiles to JDK 1.4 compatibility. You need to change this.
You need to add this to your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
[Edit: thank you Sean for pointing out Hadoop requires JDK 6]
I ran into this exact same problem. Turned out sbt itself was running on Java 5, which is the default on my Mac for a silly but valid reason. Once I changed my sbt script to explicitly start with Java6, everything worked fine.
Regardless of your maven-compiler-plugin's source & target configurations (that only controls how your own source code is compiled) you must use a 1.6 JVM to run Hadoop's code since it is compiled targetting "1.6" JVM.
So, just install a 1.6 java runtime and use that to run your program.