Maven Error: Could not find or load main class

后端 未结 12 1555
忘了有多久
忘了有多久 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:29

    The first thing i would suggest is to use the correct configuration for predefined descriptors.

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            [...]
    </project>
    

    To configure the main class you need to know the package and name of the class you would like to use which should be given into <mainClass>...</mainClass> parameter.

    Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead.

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

    I got this error using Maven, and I discovered the solution.

    Error: Could not find or load main class com.mycompany.testapifactory.Main
    

    I'm using java JDK version 1.7 on Linux, my pom.xml file was the default generated by Netbeans and I was using these commands to compile, which do work fine with a normal hello-world java application:

    mvn clean compile
    java -jar target/TestAPIFactory-1.0-SNAPSHOT.jar com.mycompany.testapifactory.Main
    

    What happened:

    It turns out my problem was that my Main method was extending something Exotic like this:

    public class Main extends SomeExoticLibraryClass{
        public static void main(String[] args){
            //...
        }
    }
    

    It was this extending of the main class that caused the above error.

    TLDR solution:

    Make sure your main class isn't extending any 3rd party classes. Refactor those out and away into their own classes. That error message is awful, and requires process of elimination to find out what to do.

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

    Unless you need the 'maven-assembly-plugin' for reasons other than setting the mainClass, you could use the 'maven-jar-plugin' plugin.

         <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <mainClass>your.package.yourprogram.YourMainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
         </plugins>
    

    You can see the plugin in practise in the ATLauncher.

    The 'mainClass' element should be set to the class that you have the entry point to your program in eg:

    package your.package.yourprogram;
    
    public class YourMainClass {
    
        public static void main(String[] args) {
            System.out.println("Hello World");
        }
    }
    
    0 讨论(0)
  • 2020-12-05 07:31

    I got it too, the key was to change the output folder from bin to target\classes. It seems that in Eclipse, when converting a project to Maven project, this step is not done automatically, but Maven project will not look for main class based on bin, but will on target\classes.

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

    this worked for me....

    I added the following line to properties in pom.xml

    <properties>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
    
    0 讨论(0)
  • 2020-12-05 07:35

    add this to your pom.xml file:

    <configuration>
       <transformers>
           <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
             <mainClass>sample.HelloWorldApplication</mainClass>
           </transformer>
       </transformers>
    </configuration>
    

    and add the class name of your project (full path) along with the package name like "com.packageName.className" which consists of the main method having "run" method in it. And instead of your "???" write ${mainClass} which will automatically get the className which you have mentioned above.

    Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"

    I hope it will work normally and server will start at the specified port.

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