How to set Java JDK for SonarScan?

Hi, i am having this issue. On my VM(dynamically deployed Windows Jenkins Node to Azure) i need have older version of Java (jdk-8u131). This version is no longer supported. Therefore i am trying to install newer Java before SonarScan and MSBuild is executed, but for whatever reason Sonar pick up always the older one. Example of Pipeline:

 stage('PreBuild') {
		dir(env.DirRoot) {										
					bat 'powershell -ExecutionPolicy Unrestricted -File SonarInstallJavaJRE11.ps1'
		}
		run_build_step("1_PerformPreBuild", "PREBUILD")
	}
	stage('Begin SonarQube + Build') {
		if (env.SonarQube.toBoolean()) {  
			withCredentials([string(credentialsId: 'XXX', variable: 'sq_secret')]){	
			
				def sq_home = tool 'SonarScannerFW4.6MS410'
				def sq_scanner = "\"$sq_home\\SonarScanner.MSBuild.exe\""

				def sq_login = "/d:\"sonar.login=$sq_secret\""

				env.SQ_RUN = "true"
				env.SQ_BEGIN = "$sq_scanner begin /k:XXXXX /o:XXXX /d:\"sonar.branch.name=${BRANCH_NAME}\" /d:sonar.showProfiling=true /d:sonar.log.level=TRACE /d:sonar.skipPackageDesign=true $sq_login $sq_exclusions $sq_file_suffixes $sq_test_params"
				env.SQ_END = "$sq_scanner end $sq_login"
			}
		}
			withSonarQubeEnv('Sonar') {
				run_build_step("2_PerformBuild", "BUILD") 
			}
	}	

SonarInstallJavaJRE11.ps1: 
.. download JDK11.. install .. replace:
$path = $Env:Path
$newpath = $path.replace("C:\ProgramData\Oracle\Java\javapath;","")
$env:Path = $newpath
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", "c:\Program Files\Java\jdk-11.0.9\", "Machine")
[System.Environment]::SetEnvironmentVariable("JAVA_HOME_11_X64", "c:\Program Files\Java\jdk-11.0.9", "Machine")
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";c:\Program Files\Java\jdk-11.0.9\bin", "Machine")
$Env:Path += ";c:\Program Files\Java\jdk-11.0.9\bin"

PATH result: echo $Env:Path
C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0;C:\windows\System32\OpenSSH;C:\Program Files (x86)\Git\cmd;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files\Microsoft Windows Performance Toolkit;C:\windows\system32\config\systemprofile.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn;c:\Program Files\Microsoft SQL Server\100\Tools\Binn;c:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\Users\Toad\AppData\Local\Microsoft\WindowsApps;C:\Users\Toad.dotnet\tools;C:\Program Files\OpenSSH-Win64;C:\windows\ServiceProfiles\SSHD\AppData\Local\Microsoft\WindowsApps;C:\Users\Toad\AppData\Local\Microsoft\WindowsApps;;c:\Program Files\Java\jdk-11.0.9\bin;C:\Users\Toad\AppData\Local\Microsoft\WindowsApps;

Java_home: attachmentjava

  • wrapper.conf cannot be located
    -sonar-project.properties doesnt hava java property

But still i am getting:
[2021-01-12T15:41:07.199Z] Calling the SonarQube Scanner…

[2021-01-12T15:41:07.766Z] INFO: Scanner configuration file: c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScannerFW4.6MS410\sonar-scanner-4.4.0.2170\bin\..\conf\sonar-scanner.properties

[2021-01-12T15:41:07.766Z] INFO: Project root configuration file: c:\jenkins\workspace\main-pipeline_master\src\.sonarqube\out\sonar-project.properties

[2021-01-12T15:41:08.030Z] 15:41:07.870 INFO: SonarScanner 4.4.0.2170

[2021-01-12T15:41:08.030Z] 15:41:07.873 INFO: **Java 1.8.0_131 Oracle Corporation (64-bit)**

[2021-01-12T15:41:08.030Z] 15:41:07.874 INFO: Windows Server 2016 10.0 amd64

Thank you for any advice.
Jakub Kremer.

Hi,

Welcome to the community!

Is $JAVA_HOME set in this environment? And if so, does it point to the old Java version?

 
Ann

Hi,
thank you!
Yes, JAVA_HOME is set and pointing to the new version. java

Hi,

That’s what I get for skimming. I see you made that clear in your OP. I’ve added the dotnet tag to this to draw the attention of the folks who are actually competent to speak in this area.

 
Ann

1 Like

Thanks.
So nobody any idea how to set JAVA for SonarScanner.MSBuild.exe?
Few more log from our build process.:

[Pipeline] tool (hide)
Unpacking https://github.com/SonarSource/sonar-scanner-msbuild/releases/download/5.0.4.24009/sonar-scanner-msbuild-5.0.4.24009-net46.zip to c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScannerFW4.6MS410 on vm-build35b4c0


[Pipeline] }10:11:06  XXX@vm-build35b4c0 c:\jenkins\workspace\ine_topic-XXX-integration-YYY\src>cmd.exe /C T:\Build\Jenkins\MSBuild\2_PerformBuild.bat 
10:11:06  ++SonarQube enabled during the build++
10:11:06  "c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScannerFW4.6MS410\**SonarScanner.MSBuild.exe" begin** /k:XXXXXX /o:oneidentity /d:"sonar.branch.name=topic-XXX-integration-YYY" /d:sonar.showProfiling=true /d:sonar.log.level=TRACE /d:sonar.skipPackageDesign=true /d:"sonar.login=******" /d:"sonar.exclusions=**/*.g.cs,**/*.resx,**/*json,**/*js,**/*xml,**/*tpl,**/*srt,**/*cbt,**/*loc,**/*cfg,**/*snippet,**/*tss,**/*ico,**/*png,**/*sql,**/*.Designer.cs,**/_AutoGenerated/**,**/DataHub/**,**/escrow/**" /d:sonar.c.file.suffixes=- /d:sonar.cpp.file.suffixes=- /d:sonar.objc.file.suffixes=- /d:sonar.cs.vstest.reportsPaths=\**\*.trx /d:sonar.cs.vscoveragexml.reportsPaths=\**\*.coveragexml
10:11:06  SonarScanner for MSBuild 5.0.4
10:11:06  Using the .NET Framework version of the Scanner for MSBuild
10:11:06  Pre-processing started.
10:11:06  Preparing working directories...
10:11:06  09:11:06.489  Updating build integration targets...
10:11:07  09:11:07.269  Fetching analysis configuration settings...
10:11:08  09:11:08.744  Provisioning analyzer assemblies for cs...
10:11:08  09:11:08.747  Installing required Roslyn analyzers...
10:11:11  09:11:10.881  Provisioning analyzer assemblies for vbnet...
10:11:11  09:11:10.881  Installing required Roslyn analyzers...
10:11:11  09:11:10.936  Pre-processing succeeded.


10:11:11  "T:\_Tools\run_msbuild.cmd" -maxCpuCount -nologo -verbosity:m -ds  "-p:BuildPlatform=x64;BuildConfiguration=Release;PatchNum=0;BuildNum=244;BetaExpiration=30;SpecialBuild=topic-XXX-integration-YYY.244;TestPlatform=x86" "T:\Build\Jenkins\MSBuild\Build.msbuild" "/p:reportanalyzer=true" "/bl:out.binlog" 

10:11:11  C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\**MSBuild.exe** -ds -nologo -maxCpuCount -p:BuildPlatform=x64;BuildConfiguration=Release;PatchNum=0;BuildNum=244;BetaExpiration=30;SpecialBuild=topic-XXX-integration-YYY.244;TestPlatform=x86 /p:reportanalyzer=true -verbosity:m /bl:out.binlog T:\Build\Jenkins\MSBuild\Build.msbuild
10:11:13    Run Build XXX with plugins and tools. Build Platform:'x64'
10:11:15    NuGet Version: 5.8.0.6930
10:11:16    MSBuild auto-detection: using msbuild version '16.3.0.46305' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\bin'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
10:11:16    MSBuild P2P timeout [ms]: 120000


Calling the SonarScanner CLI...
INFO: Scanner configuration file: c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScannerFW4.6MS410\sonar-scanner-4.4.0.2170\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: c:\jenkins\workspace\ine_topic-XXX-integration-YYY\src\.sonarqube\out\sonar-project.properties
10:12:47.065 INFO: SonarScanner 4.4.0.2170
10:12:47.068 INFO: Java 1.8.0_131 Oracle Corporation (64-bit)
10:12:47.068 INFO: Windows Server 2016 10.0 amd64
10:12:47.324 DEBUG: keyStore is : 
10:12:47.324 DEBUG: keyStore type is : jks

10:14:17.817 DEBUG: GET 200 https://sonarcloud.io/api/ce/component?component=XXXXXX | time=182ms
10:14:17.821 WARN: The version of Java (1.8.0_131) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 11.
10:14:17.986 DEBUG: GET 200 https://sonarcloud.io/api/settings/was_analyzed_during_brownout?component=XXXXXX | time=164ms
10:14:17.987 INFO: ------------- Run sensors on module Quest.Schema.Sybase.Compare
10:14:18.117 INFO: JavaScript/TypeScript frontend is enabled
10:14:18.153 INFO: Load metrics repository

11:13:22  XXX@vm-buildffd510 c:\jenkins\workspace\ine_topic-XXX-integration-YYY\src>cmd.exe /C T:\Build\Jenkins\MSBuild\3.1_EndPerformBuild.bat 
11:13:22  "c:\jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\SonarScannerFW4.6MS410**\SonarScanner.MSBuild.exe" end /d:"sonar.login=******"**
11:13:22  SonarScanner for MSBuild 5.0.4
11:13:22  Using the .NET Framework version of the Scanner for MSBuild
11:13:22  Post-processing started.

11:15:22.226 WARN: The version of Java (1.8.0_131) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 11.

Hi @J_K

It looks like the JAVA_HOME variable is not persisted (or this is another session) between your stages.

I would try to set it again in the “Begin SonarQube + Build” to see if that changes something.

Let me know.

Thanks.

OK, i can try.
Thank you.