How to solve java.lang.ClassNotFoundException when compiling with maven

Viewed 44

I'm very new to java, and the maven documentation is a bit tough for me to comprehend as a noobie.

I have a standalone Java App I'm developing on my local - with the purpose to upload a file to an azure blob that I created.

I don't think I have a great understanding of the entire maven flow in compiling + running my code, and I think that's the cause of my issue, but can't be sure. Here's my code:

package com.nrinaldi.app;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.*;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;


public class App{
    public static final String storageConnectionString = "test-string";


    public static void main( String[] args ) throws URISyntaxException, InvalidKeyException, StorageException {
        System.out.println( "Hello World! run" );

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference("nrinalditest");

        System.out.println("Creating Container: " + container.getName());
    }

}
  • 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.nrinaldi.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <name>my-app</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    
    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    
    <dependencies>
      <!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
      <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-storage</artifactId>
        <version>8.4.0</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
      </dependency>
    
    </dependencies>
    
    <build>
      <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
          <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
          <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
          </plugin>
          <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
          </plugin>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
          </plugin>
          <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
          </plugin>
          <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
          <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.7.1</version>
          </plugin>
          <plugin>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>3.0.0</version>
          </plugin>
        </plugins>
      </pluginManagement>
    </build>
    

Error I'm receiving:

Error: Could not find or load main class com.nrinaldi.app
Caused by: java.lang.ClassNotFoundException: com.nrinaldi.app

after I run java -cp target/my-app-1.0-SNAPSHOT.jar com.nrinaldi.app

Also attached is a screenshot of my directory. I saw that ultimately, the issue is because my .class file from compiled App.java isn't in the same directory, but I can't move it from target/classes/com/nrinaldi/app to src/main/java/com/nrinaldi/app.

Hoping someone can take a look and tell me what I have wrong! Thanks :-)

My directory

EDIT:

Problem was - I had no entrypoint to my class in the pom.xml. In the build step, I needed this line:

 <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.nrinaldi.app.App</mainClass>
            </transformer>
          </transformers>

I also added the shade plugin to make a Fat Jar.

0 Answers
Related