Maven Error: Could not find or load main class

后端 未结 12 1554
忘了有多久
忘了有多久 2020-12-05 07:09

I\'m using a Java Maven program and I don\'t know what to enter as the . I\'ve tried all kinds of things based off of numerous stackoverflow q

相关标签:
12条回答
  • 2020-12-05 07:12

    TLDR : check if packaging element inside the pom.xml file is set to jar.

    Like this - <packaging>jar</packaging>. If it set to pom your target folder will not be created even after you Clean and Build your project and Maven executable won't be able to find .class files (because they don't exist), after which you get Error: Could not find or load main class your.package.name.MainClass


    After creating a Maven POM project in Netbeans 8.2, the content of the default pom.xml file are as follows -

    <?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.mycompany</groupId>
       <artifactId>myproject</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>pom</packaging>
       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       </properties>
    </project>
    

    Here packaging element is set to pom. Hence the target directory is not created as we are not enabling maven to package our application as a jar file. Change it to jar then Clean and Build your project, you should see target directory created at root location. Now you should be able to run that java file with main method.

    When no packaging is declared, Maven assumes the packaging as jar. Other core packaging values are pom, war, maven-plugin, ejb, ear, rar. These define the goals that execute on each corresponsding build life-cycle phase of that package. See more here

    0 讨论(0)
  • 2020-12-05 07:17

    For me the problem was nothing to do with Maven but to do with how I was running the .jar. I wrote some code and packaged it as a .jar with Maven. I ran it with

    java target/gs-maven-0.1.0.jar
    

    and got the error in the OP. Actually you need the -jar option:

    java -jar target/gs-maven-0.1.0.jar
    
    0 讨论(0)
  • 2020-12-05 07:18

    I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.

    0 讨论(0)
  • 2020-12-05 07:18

    specify the main class location in pom under plugins

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <mainClass>com.example.hadoop.wordCount.WordCountApp</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    0 讨论(0)
  • 2020-12-05 07:20

    I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.

    0 讨论(0)
  • 2020-12-05 07:24

    Please follow the below snippet.. it works..

    <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.xyz</groupId>
        <artifactId>test</artifactId>
        <packaging>jar</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>TestProject</name>
        <description>Sample Project</description>
        <dependencies>
            <!-- mention your dependencies here -->
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.1.1</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.xyz.ABC.</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Please note, you have to provide the full classified class name (class name including package name without .java or .class) of main class inside <mainClass></mainClass> tag.

    0 讨论(0)
提交回复
热议问题