Error: Could not find or load main class com.communication.App in IntelliJ IDE

45 Views Asked by At

When executing a Jar using IntelliJ IDE I encountered the error ***"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

Process finished with exit code 1***

Refer to below screenshots. IntelliJ error screenshot

Here is my 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>

Here is my MANIFEST.MF Manifest-Version: 1.0 Main-Class: com.communication.App

Here is my 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 )
    {
        // You can get your connection string from your resource in the Azure portal.
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter connection string of your azure email service: ");
        String connectionString = scanner.nextLine();
        System.out.println("Enter sender address: ");
        String senderAddress = scanner.nextLine();
        System.out.println("Enter recipient address: ");
        String recipientAddress = scanner.nextLine();

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

        EmailMessage message = new EmailMessage()
                .setSenderAddress(senderAddress)
                .setToRecipients(recipientAddress)
                .setSubject("Welcome to Azure Communication Services Email")
                .setBodyPlainText("This email message is sent from Azure Communication Services Email using the Java SDK by Sribatsa.");

        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("Email send poller status: " + 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("Polling timed out.");
                }
            }

            if (poller.getFinalResult().getStatus() == EmailSendStatus.SUCCEEDED)
            {
                System.out.printf("Successfully sent the email (operation id: %s)", poller.getFinalResult().getId());
            }
            else
            {
                throw new RuntimeException(poller.getFinalResult().getError().getMessage());
            }
        }
        catch (Exception exception)
        {
            System.out.println(exception.getMessage());
        }
    }
}

I could see that the archive created under ~\out\artifacts\communication_quickstart_jar has the file com/communication/App.class but it keeps giving this error.

Any help would be much appreciated.

I tried to solve this by following below steps but none of them worked. Rebuild the project Rebuild Artifacts followed solutions in Error: Could not find or load main class in intelliJ IDE

I expected the jar to execute successfully

1

There are 1 best solutions below

0
Sribatsa Pradhan On

I would answer my own question. Looks like there is some maven dependency issue that caused this issue. Followed below steps to resolve it.

  1. Remove all dependencies and build from pom.xml. So the new XML will look like below
  2. Click on Maven->Reload All Maven Projects
  3. Click on File->Project Structure
  4. Click on Artifacts and remove the existing artifacts by clicking on
    • (minus) button
  5. Click on Apply and OK
  6. Add back dependencies and build in the pom.xml and follow step 2
  7. Follow step 3, click on Artifacts and then click on + (plus button) to re-add artifact by following JAR->From Modules with dependencies
  8. Select Main Class and in Directory for META-INF/MANIFEST.MF replace "java" with "resources". Refer to 5th screenshot
  9. Click on OK and OK
  10. Click on Build->Build Artifacts->Rebuild
  11. Click on Run icon towards the right of the IDE. Make sure the Jar Application is selected in the drop down. Refer to 8th screenshot

1 pom.xml maven reload changes

2 Navigate to Project Structure

3 Remove Artifacts Step

4 Add Artifact

5 JAR From Dependencies

6 Build Artifacts

7 Rebuild

8 Run