I'm using a Java Maven program and I don't know what to enter as the <mainClass>
. I've tried all kinds of things based off of numerous stackoverflow questions, but they are not solving the error.
Each time it says:
Maven Error: Could not find or load main class ...
I have this written inside my pom.xml
(minus the ???
)
<build> ... <plugins> ... <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.3</version> <configuration> <descriptors> <descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor> </descriptors> <archive> <manifest> <mainClass> ??? </mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ... </plugins> ... </build>
How do I fix these errors?
When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.
The very reason why the 'Could Not Find or Load Main Class' is thrown is because JVM was not able to find where your . class files were being stored. The easiest way to resolve this error is to control where the . class files are stored and explicitly tell JVM to look there.
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.
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"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With