Maven/Jenkins java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 - maven

I have a Jenkins server having JDK & JRE 6 and 7 installed together.
All of the projects are built on 1.6 except one which is 1.7 dependent.
I've configured the maven pom file to use the Java compiler from the JAVA_HOME_7 environment PATH.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
**<executable>${env.JAVA_HOME_7}/bin/javac</executable>**
<fork>true</fork>
<verbose>false</verbose>
</configuration>
</plugin>
During mvn install I'm getting the following error:
java.lang.RuntimeException: There was an error in the forked process
java.lang.UnsupportedClassVersionError: : Unsupported major.minor version 51.0
which I think means that the server is using JRE 1.6.
How to keep the JRE 1.6 together with 1.7 in order to keep the compatibility with the old 1.6 projects and the new 1.7 one?
Many Thanks,
Atanas

You will need to run surefire tests with java 7 too. By default surefire will use same jvm as that running maven - Java6 in your case.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
...
<jvm>${env.JAVA_HOME_7}/bin/java</jvm>
</configuration>
</plugin>
</plugins>

I remember I also struggled to this problem. Follow below steps to resolve the problem
Cause: when multiple JRE is installed then multiple java.exe is also installed to many location of system.
Solution: Modify your environment PATH variable and change the order of java.exe. put location of java.exe on first position like below code
PATH = C:\Program Files\Java\jdk1.6.0\; other;other;other
Change above location according to your use and installation location.

I have encountered this problem more than once, it is because you have more than one versions of jdk(jre) on your system, so just set the JAVA_HOME to the proper jdk you compile your project with and the running would be fine.

Have a look at your target/lib directory, you might have two versions of same jar. For me it was creating the pro

Related

Confusion on maven compiler plugin

I am quite confused about maven compiler plugin and what it does. I have a project that has several modules. In my top pom.xml I have a section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
My understanding is this specifies the JDK compiler version used to compile the code, and this section get inherited by all the modules. What I don't get is in my IntelliJ IDEA I can still specify project JDK in the settings and it seems like that setting overrides this. When I run maven install in the IDE I can confirm that it is using javac from JDK 8 to compile. So what does this section do exactly?
You are correct; maven-compiler settings should be inherited by child modules.
I don't know about IntelliJ, but I can tell you that Eclipse picks&chooses whatever it wants from maven config, and for all the rest it uses its own settings.
Therefore, I'd expect IntelliJ may do something similar?
The simplest way to test this is to run a mvn clean install via command line, and see which "wins". If you get artifact compiled with 1.8 then it means you're missing something in Maven config which causes those settings not to propagate to children. If you get artifact compiled with 1.7 then it is IntelliJ who does it and not maven-compiler-plugin.

Sonarqube-Analysis on Jenkins fails due to PMD Plugin throwing UnsupportedClassVersionError: Unsupported major.minor version 52.0

Java-Version Configuration
Java-Version used for compiling classes with ant: 1.8
pom.xml für Sonarqube-Analysis defines:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
and
<!-- Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. -->
<sonar.java.source>1.8</sonar.java.source>
Sonarqube-Version: 4.5.4
Sonarqube-PMD-Plugin-Version: 2.4.1
JDK configured in Jenkins-Job: 1.8
Sonarqube-Server runs using Java 8
There is only Java8 installed on both Jenkins- and Sonarqube-Server.
Jenkins executes the following maven-comand to start the analysis:
mvn.bat -f "D:\Jenkins\workspace\DVLP MW2.0 Coverage und Codeanalyse\sonar-pom.xml" -e -B sonar:sonar -Dsonar.jdbc.driver=com.mysql.jdbc.Driver "-Dsonar.jdbc.url=jdbc:mysql://192.168.58.121:3306/sonar?autoReconnect=true&useUnicode=true&characterEncoding=utf8" -Dsonar.host.url=http://192.168.58.121/sonar
What are we missing?
You should remove property sonar.java.source. SonarQube gets Java Class Version from maven-compiler-plugin configuration. It is not a problem if you compile with JDK7 and run analysis with JDK8 (or do both actions with JDK7/JDK8).

jdk version for compiling java code: jenkins vs pom.xml

We are using jdk 6 to compile our code (specified in jenkins), I am trying to specify jdk 7 to compile the projects from now on. I noticed some of the projects have specified jdk 6 in their pom.xml. I am not sure which one takes precedence when jenkins specifies jdk 7 but pom.xml speicifies jdk 6. Any idea which one takes the final precedence to compile the code?
In JDK you can specify the generated class files that target a specified version of the VM. For example if JDK 1.7 is used you can specify the target class file version as 1.7 or lower version.
In maven compiler plugin you can specify the source and target version as follows.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${sourceVersion}</source>
<target>${targetVersion}</target>
</configuration>
</plugin>
</plugins>
</build>
In your case if you want the class to be compiled to java 1.7 then you have to specify ${sourceVersion} to 1.7 and the ${targetVersion} to 1.7
I am not sure which one takes precedence when jenkins specifies jdk 7 but pom.xml speicifies jdk 1.6
If JDK 7 is used in Jenkins and the target version in the pom is 1.6 then the classes will be compiled to jdk 1.6 version.
Check the following links for more information.
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#options

Maven : error: generics are not supported in -source 1.3 , I am using 1.6

I have imported an existing Maven project into Eclipse IDE .
I have modified some code in it , it compiled successfully ,
I am using Java 1.6 as compiler
and when i am trying to run maven clean install -X
Its giving the following error
could not parse error message: (use -source 5 or higher to enable generics)
D:\bayer\util\src\main\java\com\tata\bayer\util\BrokerageCalendar.java:179: error: generics are not supported in -source 1.3
private static Hashtable<String, Boolean> nyseHolidays = new Hashtable<String, Boolean>();
^
could not parse error message: (use -source 5 or higher to enable generics)
D:\bayer\util\src\main\java\com\tata\bayer\util\APIHttpXmlClient.java:27: error: generics are not supported in -source 1.3
Class<? extends APIResponse> responseClass) {
^
Please suggest any ideas as how to resolve this ??
Did you declare that you want to use java 1.6 in your project pom.xml?:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument></compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Configuring the Maven Compiler Plugin will fix the problem. It turns out the problem was caused by the Maven3 package in the Ubuntu repository. An alternate fix is to download Maven 3 from the Apache website which uses a more up to date Compiler plugin.
I wanted to know why this was happening when the documentation states the default Java source is 1.5. To see what mvn is using for your compiler plugin use:
mvn help:effective-pom
My Maven Compiler Plugin was 2.0.2 even though I was using Maven 3.0.4 from the Ubuntu packages.
When I run the same command using Maven 3.0.4 from Apache I have a plugin version 2.3.2, which defaults to Java 1.5 as expected.
You have to configure the Maven Compiler Plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Another way that doesn't involve modifying the pom is to specify the source and target in the command line:
mvn clean install -Dmaven.compiler.source=1.6 -Dmaven.compiler.target=1.6
Note that this should be avoided generally as the build cannot be guaranteed to be repeatable this way.
I'd prefer this:
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
...

Maven Compiler Plugin Fork Option Not Working

I have a maven pom file with the following compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<executable>${path_to_JDK6}</executable>
<fork>true</fork>
<compilerVerison>1.6</compilerVerison>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
My system's JAVA_HOME is set to a 1.5 JDK. When I run mvn package, maven throws the following error message:
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac <options> <source files>
Does anyone have any ideas about why Maven is using the 1.5 JDK instead of forking to the 1.6 executable? Is there any debugging option that I could use? Are the fork and executable options broken in Maven 2?
Note: My system admin would not allow me to change the value of JAVA_HOME and some of my new libraries are written for java 6. So I would like to find a workaround to make maven compile with JDK 1.6.
My ${path_to_JDK6} variable only included the path to the folder containing the JDK. It did not include the /bin/javac. When I added /bin/javac, the 1.6 compiler was invoked.

Resources