You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Maven项目连接Azure Cosmos编译报错:com.azure.cosmos包不存在

解决Java + Maven连接Azure Cosmos时的编译错误:package com.azure.cosmos does not exist

在IntelliJ环境下使用Java + Maven连接Azure Cosmos并检索数据时,遇到编译错误:java: package com.azure.cosmos does not exist,期望项目正常运行无报错,相关项目文件如下:

1. POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.beginsecure</groupId>
    <artifactId>CosmosUtility</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-sdk-bom</artifactId>
                <version>1.2.23</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-cosmos</artifactId>
            <version>4.58.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.32</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20240303</version>
        </dependency>

    </dependencies>
</project>

2. CosmosHelper.java

package com.beginsecure;
import com.azure.identity.ChainedTokenCredential;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.cosmos.*;
import com.azure.cosmos.CosmosClient;

import com.azure.identity.DefaultAzureCredentialBuilder;
public class CosmosHelper {
    private CosmosAsyncClient client;
    private String databaseName;

    public CosmosHelper(String cosmosEndPoint, String managedIdentity, String dbName) {
        System.out.printf(dbName);
        DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
                .managedIdentityClientId(managedIdentity)
                .build();
        this.client = new CosmosClientBuilder()
                .endpoint(cosmosEndPoint)
                .credential(defaultCredential)
                .buildAsyncClient();
        this.databaseName = dbName;
    }
}

3. Main.java

package com.beginsecure;

public class Main {
    public static void main(String[] args) {
        System.out.printf("Hello and welcome!");
        CosmosHelper tst=new CosmosHelper("https://testdb.documents.azure.com:443/","<Synapse-Work-Space-Identity>","ToDoList");

        for (int i = 1; i <= 5; i++) {
            System.out.println("i = " + i);
        }
    }
}

解决步骤

1. 刷新Maven依赖

执行以下命令强制更新依赖并重新构建项目:

mvn clean install -U

或者在IntelliJ右侧Maven面板中点击刷新按钮(🔄),确保azure-cosmos包被正确下载到本地仓库。

2. 清理IntelliJ缓存并重启

打开File > Invalidate Caches...,选择Invalidate and Restart,解决缓存导致的依赖识别异常。

3. 检查Java版本兼容性

Azure Cosmos SDK v4支持Java 8及以上版本,但高版本(如Java 20)可能存在兼容性问题,建议修改POM中的Java版本为LTS版本(如Java 17):

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

4. 调整Azure SDK BOM依赖

当前POM中azure-sdk-bom版本1.2.23与azure-cosmos4.58.0版本不匹配,可选择两种处理方式:

  • 移除BOM依赖:删除dependencyManagement中的azure-sdk-bom配置,让单个依赖使用指定版本;
  • 更新BOM版本:将BOM版本更新为与azure-cosmos4.58.0对应的版本。

修改后的POM示例(移除BOM):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.beginsecure</groupId>
    <artifactId>CosmosUtility</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-cosmos</artifactId>
            <version>4.58.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.32</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20240303</version>
        </dependency>
    </dependencies>
</project>

内容的提问来源于stack exchange,提问作者Ajith Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:45:55