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

IntelliJ执行Jar报错:无法找到或加载主类com.communication.App

执行Jar包时找不到主类com.communication.App的问题

执行Jar包时出现以下错误:

"C:\Program Files\Zulu\zulu-17\bin\java.exe" -Dfile.encoding=windows-1252 -jar C:\Users\srpradhan\Desktop\learning\Java\communication-quickstart\out\artifacts\communication_quickstart_jar\communication-quickstart.jar
Error: Could not find or load main class com.communication.App
Caused by: java.lang.ClassNotFoundException: com.communication.App

进程以退出码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.communication</groupId>
    <artifactId>App</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-communication-email</artifactId>
            <version>1.0.0-beta.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

MANIFEST.MF内容

Manifest-Version: 1.0
Main-Class: com.communication.App

App.java代码

package com.communication;

import com.azure.communication.email.models.*;
import com.azure.communication.email.*;
import com.azure.core.util.polling.LongRunningOperationStatus;
import com.azure.core.util.polling.PollResponse;
import com.azure.core.util.polling.SyncPoller;
import java.util.Scanner;

import java.time.Duration;

public class App
{
    public static void main( String[] args )
    {
        // 可从Azure门户的资源中获取连接字符串
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入Azure邮件服务的连接字符串: ");
        String connectionString = scanner.nextLine();
        System.out.println("输入发件人地址: ");
        String senderAddress = scanner.nextLine();
        System.out.println("输入收件人地址: ");
        String recipientAddress = scanner.nextLine();

        EmailClient emailClient = new EmailClientBuilder()
                .connectionString(connectionString)
                .buildClient();

        EmailMessage message = new EmailMessage()
                .setSenderAddress(senderAddress)
                .setToRecipients(recipientAddress)
                .setSubject("欢迎使用Azure通信服务邮件")
                .setBodyPlainText("此邮件由Sribatsa通过Java SDK从Azure通信服务邮件发送。");

        try
        {
            SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message, null);

            PollResponse<EmailSendResult> pollResponse = null;

            Duration timeElapsed = Duration.ofSeconds(0);
            Duration POLLER_WAIT_TIME = Duration.ofSeconds(10);

            while (pollResponse == null
                    || pollResponse.getStatus() == LongRunningOperationStatus.NOT_STARTED
                    || pollResponse.getStatus() == LongRunningOperationStatus.IN_PROGRESS)
            {
                pollResponse = poller.poll();
                System.out.println("邮件发送轮询状态: " + pollResponse.getStatus());

                Thread.sleep(POLLER_WAIT_TIME.toMillis());
                timeElapsed = timeElapsed.plus(POLLER_WAIT_TIME);

                if (timeElapsed.compareTo(POLLER_WAIT_TIME.multipliedBy(18)) >= 0)
                {
                    throw new RuntimeException("轮询超时。");
                }
            }

            if (poller.getFinalResult().getStatus() == EmailSendStatus.SUCCEEDED)
            {
                System.out.printf("邮件发送成功(操作ID: %s)", poller.getFinalResult().getId());
            }
            else
            {
                throw new RuntimeException(poller.getFinalResult().getError().getMessage());
            }
        }
        catch (Exception exception)
        {
            System.out.println(exception.getMessage());
        }
    }
}

现状与已尝试方案

  • 已确认~\out\artifacts\communication_quickstart_jar目录下的Jar包中存在com/communication/App.class文件,但仍报错
  • 已尝试的解决方法均无效:
    • 重建项目
    • 重建工件
    • 参考Stack Overflow相关解决方案

期望Jar包能成功执行,恳请提供帮助。


内容的提问来源于stack exchange,提问作者Sribatsa Pradhan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 07:40:05