javaspring-bootazureazure-iot-hubazure-iot-sdk

ClassNotFoundException: com.microsoft.azure.sdk.iot.deps.util.Base64


I am working on a Java Spring Boot application that interacts with Azure IoT Hub. When I run my test, I get the following error

java.lang.NoClassDefFoundError: com/microsoft/azure/sdk/iot/deps/util/Base64

java.lang.ClassNotFoundException: com.microsoft.azure.sdk.iot.deps.util.Base64
    at com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken.buildToken(ProvisioningSasToken.java:90) ~[provisioning-service-client-1.3.0.jar:na]
    at com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken.<init>(ProvisioningSasToken.java:70) ~[provisioning-service-client-1.3.0.jar:na]
    at com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp.request(ContractApiHttp.java:140) ~[provisioning-service-client-1.3.0.jar:na]
    at com.microsoft.azure.sdk.iot.provisioning.service.IndividualEnrollmentManager.createOrUpdate(IndividualEnrollmentManager.java:98) ~[provisioning-service-client-1.3.0.jar:na]
    at com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient.createOrUpdateIndividualEnrollment(ProvisioningServiceClient.java:224) ~[provisioning-service-client-1.3.0.jar:na]
    at com.example.iotproject.Service.DeviceRegistrationService.registerDevice(DeviceRegistrationService.java:42) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.


 public String registerDeviceDps() throws ProvisioningDeviceClientException, ProvisioningServiceClientException {
    try {
        String registrationId = Utils.generateRegistrationId();
        logger.info("Generated Registration ID: {}", registrationId);
        ProvisioningServiceClient provisioningClient = ProvisioningServiceClient.createFromConnectionString(dpsConnectionString);

        Attestation attestation = new SymmetricKeyAttestation(primaryKey, null);
        IndividualEnrollment enrollment = new IndividualEnrollment(registrationId, attestation);
        IndividualEnrollment result = provisioningClient.createOrUpdateIndividualEnrollment(enrollment);

        logger.info("Smart TV successfully enrolled with Registration ID: {}", result.getRegistrationId());
        return registrationId;

    } catch (ProvisioningServiceClientException e) {
        logger.error("Error during pre-enrollment: {}", e.getMessage(),e);
        throw new RuntimeException("Failed to enroll device in DPS", e);

    }

here are my dependecies

<dependency>
        <groupId>com.microsoft.azure.sdk.iot</groupId>
        <artifactId>iot-service-client</artifactId>
        <version>1.30.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.azure.sdk.iot</groupId>
        <artifactId>iot-device-client</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.azure.sdk.iot</groupId>
        <artifactId>iot-deps</artifactId>
        <version>0.15.3</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.azure.sdk.iot.provisioning</groupId>
        <artifactId>provisioning-service-client</artifactId>
        <version>1.3.0</version>
    </dependency>

Why am I getting this NoClassDefFoundError for Base64? Do I need to include another Azure SDK dependency to resolve this? Is there a better way to handle this issue?


Solution

  • You have used multiple iot dependencies. Why are their versions inconsistent? This might be a problem caused by Maven dependency conflicts. Try using the same version of iot dependencies. You can refer to the following link for more information: azure-iot-sdk-java