Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find the main class after converting java project to maven

I have a very simple HelloWorld code in Java which works ok. I'm using Eclipse and trying to figure out how to import dependencies for a project with the maven2 eclipse plugin.

    public class testMavenDep {

        public static void main(String arg[]){
            System.out.println("Hello World");
        }
    }

However, when I right click on the project > configure > convert to maven project, and then try and run it gives me an error message saying...

Could not find the main class: testMavenDep.testMavenDep. Program will exit.

And the following in the console...

java.lang.NoClassDefFoundError: testMavenDep/testMavenDep Caused by: java.lang.ClassNotFoundException: testMavenDep.testMavenDep at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Exception in thread "main"

My pom file is...

<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>testMavenDep</groupId>
  <artifactId>testMavenDep</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

My question is, for an already existing Java Project, what is the proper way to add maven dependencies? I can add the dependencies using the above method but I'm getting issues with it losing track of the main class. Thanks in advance!

like image 986
Daniel Avatar asked May 28 '11 22:05

Daniel


People also ask

Can Java project convert to Maven?

Procedure. Right-click the project and select Configure > Convert to Maven Project. Complete the Maven POM dialog. Enter a Group Id, Artifact Id, and Version or accept the defaults.

How do I run a main class jar in Maven?

In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project's dependencies as the classpath. As shown above, we're using the exec. mainClass system property to pass the fully qualified class name.

Where is the main class in spring boot application?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

How do I run a Maven project with Maven in Eclipse?

Building and Running the Maven Project in Eclipse To run the maven project, select it and go to “Run As > Java Application”. In the next window, select the main class to execute. In this case, select the App class and click on the Ok button. You will see the “Hello World” output in the Console window.


1 Answers

What is the source folder that you are putting your main class in? By default, Eclipse puts it in src, but maven conventions are src/main/java. It could be that adding maven dependencies is changing your source folders so that your class is not compiled.

like image 194
Andrew Eisenberg Avatar answered Sep 22 '22 14:09

Andrew Eisenberg