I am sorry that the withOSDiskEncryptionSettings
does not work as you wanted.
Actually, it will be used when you create a VM with an uploaded pre-encrypted OS disk. As it is a pre-encrypted disk, Azure will not be able to decrypt the data and the VM will not be able to start up. So, in this case, you need to tell Azure the disk encryption settings. For more details, you may refer to: Prepare a pre-encrypted Linux VHD and Specify a secret URL when you attach an OS disk
However, for a new-created Azure VM, currently, you should use disk encryption extension to enable disk encryption. And it is actually what you do from Azure portal, Azure CLI or Azure PowerShell. For example, with Azure PowerShell, you should run : Set-AzVMDiskEncryptionExtension -ResourceGroupName MyResourceGroup -VMName "MyVM" -DiskEncryptionKeyVaultUrl $KeyVault.VaultUri -DiskEncryptionKeyVaultId $KeyVault.ResourceId -SkipVmBackup -VolumeType All
So, basically, if you do want to enable disk encryption programmatically. You may use Azure Java SDK to add an extension to your VM. Here is a sample: ManageVirtualMachineExtension.java
Hope the information above would be helpful to you.