0

This will probably be flagged as duplicate but I have gone through a lot of answers in SOF and none of them worked for me. I had a Java project to which I added Maven nature, and since then I cannot run any Java file. Upon running (with -Xdiag) it says,

    Error: Could not find or load main class main.HelloWorld
Caused by: java.lang.ClassNotFoundException: main.HelloWorld
java.lang.ClassNotFoundException: main.HelloWorld
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:466)
    at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:780)
    at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:675)

If I remove the Maven nature and run, it will run as expected, but when I add Maven, the issue is back again. so to my knowledge, it should not be an issue related to Java build path or compiler configuration. I am not sure what is the issue. I have tried updating the project multiple times. I have also downloaded Maven and added it to User Environment variables, if that makes a difference.

Additional information: This is my classpath file

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
        <attributes>
            <attribute name="module" value="true"/>
        </attributes>
    </classpathentry>

    <classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="org.eclipse.jst.component.nondependency" value=""/>
        </attributes>
    </classpathentry>
    <classpathentry exported="true" kind="lib" path="C:/Users/Anubhab Giri/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.48/tomcat-embed-core-9.0.48.jar" sourcepath="C:/Users/Anubhab Giri/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.48/tomcat-embed-core-9.0.48-sources.jar">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="maven.groupId" value="org.apache.tomcat.embed"/>
            <attribute name="maven.artifactId" value="tomcat-embed-core"/>
            <attribute name="maven.version" value="9.0.48"/>
            <attribute name="maven.scope" value="compile"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>

This is my project file

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>hello-world</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>
  • 1
    Does this answer your question? [Eclipse "Error: Could not find or load main class"](https://stackoverflow.com/questions/11235827/eclipse-error-could-not-find-or-load-main-class) – Arsh Coder Oct 08 '21 at 03:14
  • I have seen this already, the selected answer talks about modifying the class path attributes, but I have not migrated my project from anywhere, it was built locally. Secondly, I am not using any jar unlike the other user. I have added my class path config for reference. – Anubhab Giri Oct 08 '21 at 04:40
  • I assume you have "Build Automatically" set? When you add the Maven nature, and it builds your project, does it show any red marks in any file in your project, like the pom.xml? Can you show your pom.xml file? – David M. Karr Oct 08 '21 at 05:15
  • According to your `.classpath` file, your only source folder is `src`, but in Maven the default source folder is `src/main/java`. It's unclear how you configured your project (in which folder(s) are your Java files?). Better follow the documentation instead of following SO answers that might be wrong for the version you use. Adding or removing natures is not the recommended way and should not be necessary (that sounds like weird SO answers, not like the documentation which you should read first). – howlger Oct 08 '21 at 06:28
  • @DavidM.Karr yes, build automatically is set and there is no error after building. Here is my POM https://codeshare.io/xvg33P. – Anubhab Giri Oct 08 '21 at 06:53
  • @howlger, thanks for the direction. I will start looking into the documentation. my Java files are inside src/main/ – Anubhab Giri Oct 08 '21 at 06:56
  • `` says otherwise (`path="src"` instead of `path="src/main/java"`). The folder location must match the package, for example for the public class `com.example.Foo` the source file is `/com/example/Foo.java`. – howlger Oct 08 '21 at 08:12
  • @howlger, thank you for your suggestions. I went through Maven documentation, built a new project from scratch, and now I can run Java files. I checked the generated class path file and it has ` path="src/main/java" `. My understanding is that since I converted a Java project into a Maven project, something did not happen correctly. – Anubhab Giri Oct 08 '21 at 14:20
  • Did you convert it via right-clicking the project and choosing _Configure > Convert to Maven Project_? – howlger Oct 08 '21 at 16:55
  • @howlger, yes that's correct. You said that is not recommended. Can you elaborate a bit on why that is? – Anubhab Giri Oct 09 '21 at 01:58
  • Right-clicking the project and choosing _Configure > Convert to Maven Project_ is the recommended way and should work. I don't know why it was not working in your case. Do you have an example how to reproduce this issue? Not recommended is to add/remove project natures via _Project > Properties: Project Natures_. – howlger Oct 09 '21 at 18:11

0 Answers0