Linkage error in sonar

when I use below task above sonar task , sonar tasks are passing:

    - task: JavaToolInstaller@0
        displayName: 'Use Java 21 - Windows x64'
        inputs:
          versionSpec: 21
          jdkArchitectureOption: x64
          jdkSourceOption: LocalDirectory
          jdkFile: 'C:\Program Files\Java\openjdk-21.0.2_windows-x64_bin.zip'
          jdkDestinationDirectory: '$(Agent.TempDirectory)/'
          cleanDestinationDirectory: false

but when I replace above task with powershell task below

- task: PowerShell@2
        displayName: 'Set JAVA_HOME and PATH'
        inputs:
          targetType: 'inline'
          script: |
            if ("${{ parameters.jdk_version }}" -eq "jdk17") {
              $env:JAVA_HOME = "C:\Java\jdk17"
              $env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
              Write-Host "Using JDK 17"
            }
            elseif ("${{ parameters.jdk_version }}" -eq "jdk21") {
              $env:JAVA_HOME = "C:\Java\jdk21"
              $env:PATH = "$env:JAVA_HOME\bin;$env:PATH"
              Write-Host "Using JDK 21"
            }
            else {
              Write-Error "Unsupported JDK version: ${{ parameters.jdk_version }}"
              ex

Sonar tasks are failing with following error:

##[error]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[debug]Processed: ##vso[task.logissue type=error;]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[error]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[debug]Processed: ##vso[task.logissue type=error;]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
	java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[error]The SonarScanner did not complete successfully
##[debug]Processed: ##vso[task.logissue type=error;]The SonarScanner did not complete successfully
The SonarScanner did not complete successfully
##[error]10:19:04.589  Post-processing failed. Exit code: 1
##[debug]Processed: ##vso[task.logissue type=error;]10:19:04.589  Post-processing failed. Exit code: 1
10:19:04.589  Post-processing failed. Exit code: 1
##[debug]Exit code 1 received from tool 'C:\azp\agent\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\6.3.4\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe'
##[debug]STDIO streams have closed for tool 'C:\azp\agent\_work\_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\6.3.4\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe'
##[warning]Can't find loc string for key: LIB_ProcessExitCode
##[debug]Processed: ##vso[task.issue type=warning;]Can't find loc string for key: LIB_ProcessExitCode

Agent machine already have JDK 8, 11, 17 and 21 versions installed.

Please suggest why it is failing and fix for it?

we want to make sonar tasks to pass with out using java installer task,

below is the task fyi:

  - task: SonarQubeAnalyze@6
    displayName: 'SonarQube Run Code Analysis'

When you use the JavaToolInstaller task, it sets JAVA_HOME and PATH for all subsequent steps (jobs/tasks) in a persistent way that Azure DevOps understands.

But when using a PowerShell task, setting $env:JAVA_HOME and $env:PATH by default only affects the current PowerShell process (not globally, or for future pipeline tasks).

Thus, when the SonarQube task runs, it does not actually see JAVA_HOME and PATH pointing to your new version, and defaults to an old Java version it finds.

To persist the environment variables for the following tasks, you must explicitly tell the Azure DevOps pipeline agent to set JAVA_HOME and PATH for subsequent tasks. You do this by using the ##vso[task.setvariable] logging command in your PowerShell script:

- task: PowerShell@2
  displayName: 'Set JAVA_HOME and PATH'
  inputs:
    targetType: 'inline'
    script: |
      if ("${{ parameters.jdk_version }}" -eq "jdk17") {
        $jdkHome = "C:\Java\jdk17"
        Write-Host "Using JDK 17"
      }
      elseif ("${{ parameters.jdk_version }}" -eq "jdk21") {
        $jdkHome = "C:\Java\jdk21"
        Write-Host "Using JDK 21"
      } else {
        Write-Error "Unsupported JDK version: ${{ parameters.jdk_version }}"
        exit 1
      }
      Write-Host "##vso[task.setvariable variable=JAVA_HOME;isOutput=true]$jdkHome"
      Write-Host "##vso[task.setvariable variable=PATH;isOutput=false]$jdkHome\bin;$env:PATH"

You can also pass JAVA_HOME and modify PATH at the job level if needed.

Then, SonarQubeAnalyze should understand where to look for Java.

I have added as you suggested

now I am seeing below error

#[error]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[debug]Processed: ##vso[task.logissue type=error;]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[error]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[debug]Processed: ##vso[task.logissue type=error;]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
	java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[error]The SonarScanner did not complete successfully
##[debug]Processed: ##vso[task.logissue type=error;]The SonarScanner did not complete successfully
The SonarScanner did not complete successfully
##[error]04:34:20.262  Post-processing failed. Exit code: 1

FYI: here is the build template:

The build template you’ve shared doesn’t include the SonarQubeAnalyze task.

You’re gonna have to do a little troubleshooting yourself. What is the value of JAVA_HOME right before you run the SonarQubeAnalyze task?

  1. I have fetched the value and it is JAVA_HOME is: C:\Program Files\Java\jdk-11.0.6+10

2.Below is the build template and where you can see below sonarqubeanalyize task

This suggests to me that you need to take some extra steps to make sure that nested pipelines do not automatically have access to the variables you’ve set.

In any case – your challenge is not really a SonarQube one, it’s makign sure that JAVA_HOME is set appropriately before that task runs.

Don’t forget you can also tell SonarQubeAnalyze to look for a different environment variable by adjusting jdkVersion. (docs)

in that case, why I am seeing different error when using 7 version for qubeanlysis task which i mentioned in below ticket, configuration is same but i changed the version to 7
Scanner mode was not defined error - SonarQube Server / Community Build - Sonar Community

Now I am seeing this error Coverage report conversion completed successfully.
The TFS Processor has finished
Calling the SonarScanner CLI…

ERROR: JAVA_HOME exists but does not point to a valid Java home
folder. No “\bin\java.exe” file can be found there.

Hi Collin,
I was able to fix the java path , but sonar is passing when I choose 17 and 21 JDK versions and it is failing with below error when i choose 8 and 11 JDK versions,

For 11:

#[error]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[debug]Processed: ##vso[task.logissue type=error;]Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
Error: LinkageError occurred while loading main class org.sonarsource.scanner.cli.Main
##[error]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[debug]Processed: ##vso[task.logissue type=error;]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
	java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
##[error]The SonarScanner did not complete successfully
##[debug]Processed: ##vso[task.logissue type=error;]The SonarScanner did not complete successfully
The SonarScanner did not complete successfully
##[error]01:57:17.867  Post-processing failed. Exit code: 1
##[debug]Processed: ##vso[task.logissue type=error;]01:57:17.867  Post-processing failed. Exit code: 1
01:57:17.867  Post-processing failed. Exit code: 1
##[debug]Exit code 1 received from tool 'C:\azp\agent\_work\_tasks\SonarQubePrepare_15b

For 8:

	C:\azp\agent\_work\_temp\ContainerAdministrator_C91430E07ED4_2025-07-08_02_02_16\In\C91430E07ED4\ContainerAdministrator_C91430E07ED4_2025-07-08.02_02_16.coverage
Unique coverage files: count=1
	C:\azp\agent\_work\_temp\768a38ba-7e78-4b97-af74-9018004db19a\ContainerAdministrator_C91430E07ED4_2025-07-08.02_02_16.coverage
Converting coverage file 'C:\azp\agent\_work\_temp\768a38ba-7e78-4b97-af74-9018004db19a\ContainerAdministrator_C91430E07ED4_2025-07-08.02_02_16.coverage' to 'C:\azp\agent\_work\_temp\768a38ba-7e78-4b97-af74-9018004db19a\ContainerAdministrator_C91430E07ED4_2025-07-08.02_02_16.coveragexml'.
Coverage report conversion completed successfully.
The TFS Processor has finished
Calling the SonarScanner CLI...
##[debug]Agent environment resources - Disk: C:\ Available 125241.50 MB out of 129918.98 MB, Memory: Used 39501.00 MB out of 65534.00 MB, CPU: Usage 22.00%
##[error]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
##[debug]Processed: ##vso[task.logissue type=error;]java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
java.lang.UnsupportedClassVersionError: org/sonarsource/scanner/cli/Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
##[error]at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)

Please suggest fix for this