Need help for sonar target configuration using ant

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension) - using sonarqube-ant-task-2.5.jar
  • what are you trying to achieve - trying to get the sonar report for my corporate sonar dashboard
  • what have you tried so far to achieve this - getting ‘java.lang.UnsupportedClassVersionError: Bad version number in .class file’ error for sonar target

Hi Sonar Qube Team,

I am trying to get the sonar report for my Ant project. Below are the specification which I did in my build.xml file.

Sonar Analysis started**













java version : {java.version}</echo> <echo>ant java version : {ant.java.version}

**************** sonar analysis End******************

For all **** I have provided the correct desired values. My project is successfully completed the ‘clean’ and ‘compile’ targets but when it is running the ‘sonar’ target. It is giving me error at Line No -

PFB the stack trace -

[echo] java version : 1.5.0_85
[echo] ant java version : 1.5

sonar:

BUILD FAILED
/proj/com/src/build/build.xml:147: java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:626)
at org.apache.tools.ant.AntClassLoader.defineClassFromData(AntClassLoader.java:1128)
at org.apache.tools.ant.AntClassLoader.getClassFromStream(AntClassLoader.java:1299)
at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1355)
at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1315)
at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1068)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.tools.ant.taskdefs.Definer.addDefinition(Definer.java:594)
at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:239)
at org.apache.tools.ant.taskdefs.Antlib.execute(Antlib.java:176)
at org.apache.tools.ant.taskdefs.Definer.loadAntlib(Definer.java:443)
at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:292)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

While posting my query, the sonar specifications are missed.
Below are the specification which I did in my build.xml file.

Sonar Analysis started**













java version : {java.version}</echo> <echo>ant java version : {ant.java.version}

                           <!-- Execute Sonar -->
                            <sonar:sonar />
            </target>

**************** sonar analysis End******************

Don’t know what issue is going on this community, it is hiding some lines.
so uploaded the screenshot.

while sonar target is running it is giving error at Line - taskdef uri

Hi,

you didn’t mention the version of your Sonarqube instance, assuming you use the currrent LTS
you get the java.lang.UnsupportedClassVersionError because the ant task is compiled for a higher Java version and your Java version 1.5.0_85 should not be used anymore, see https://docs.sonarqube.org/latest/requirements/requirements/
The analysis (your ant build starting the sonar ant task) must run with Java 8
Also you have to use the most recent version of sonar ant task
=> https://binaries.sonarsource.com/Distribution/sonarqube-ant-task/sonarqube-ant-task-2.6.0.1426.jar

Gilbert

2 Likes

Hi Gilbert,

Thank you for your help!

I am using the Sonar LTS version as 7.3.0.15553.
So could you please help me the minimum supported java version and sonar ant task for the same.

I need to analyse the source code of my project which is currently compiled in java 1.6. How I can do this? Do I need to use lower version of Sonarqube server ?

Hi,

i recommend updating your Sonarqube instance to the latest LTS, which is 7.9.1
get it here https://www.sonarqube.org/downloads/
you have to use the latest version of Sonarqube ant task
https://binaries.sonarsource.com/Distribution/sonarqube-ant-task/sonarqube-ant-task-2.6.0.1426.jar
Sonarqube server requires Java 11 to run, analysis needs Java 8 at least
https://docs.sonarqube.org/latest/setup/upgrade-notes/
SonarQube 7.9 LTS Released

Note
MySQL is not supported anymore
If using sonar.search.javaOpts in $SONARQUBE_HOME/conf/sonar.properties
the Xms and Xmx parameters have to be equal, default is

sonar.search.javaOpts=-Xms2G -Xmx2G -XX:+HeapDumpOnOutOfMemoryError

Gilbert

1 Like