简体   繁体   中英

Java Eclipse Maven Could not find or load main class with jdk

In Eclipse I am using JavaFX, Maven with jre 1.8.0_45.

Normally, when I try to compile with Maven this way (Run As -> Maven compile) it will throw an error for not using the jdk.

When I change it to jdk (everywhere like in configuration, window - properties, maven configuration) and try to run/build/compile/whatever I get the error

Error: Could not find or load main class

Without giving me any class up there. Just this.

I kind o' fixed this by adding to my pom the path to javac and staying with the jre package

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler.version}</version>
    <configuration>

        <!-- With JRE: -->
        <fork>true</fork>
        <executable>C:\Program Files\Java\jdk1.8.0_45\bin\javac.exe</executable>

        <source>${java.version}</source>
        <target>${java.version}</target>
    </configuration>
</plugin>

This works.

But still I wish to know - why is this error happening? What am I doing wrong?

I've used Project -> Clean and Maven -> Update. Restarted Eclipse many times. Closed and opened the project. My Main class is given in my Run Configuration (application.Main).

EDIT:

Resolved. Answered my (own) question below.

Hey Pleased set the path environment variable mentioning the installtion path of java.

https://www.java.com/en/download/help/path.xml

Could not find or load main class HelloWorldApp.class

A common mistake made by programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp . Remember, the argument is the name of the class that you want to use, not the filename.

Resource Link:

  1. https://docs.oracle.com/javase/tutorial/getStarted/problems/
  2. https://bbs.archlinux.org/viewtopic.php?id=187463

I managed to resolve the problem.

What I did was a uninstall and a clean install of jdk, a clean install of eclipse and all eclipse plugins I was using.

Possible reasons for the problem:

  1. Some path problem and/or confusion in jdk/jre versions. @Lokesh Nandanwar was close.
  2. Some kind of eclipse problem, don't know what though

Thank you for your answers. Usually this is a CLASSPATH problem, but not this time.

Conclusion is:

If the Error is "Could not find or load main class SomeMainClass.class" you have a classpath problem or have not used java(c) command line properly. See @SkyWalker 's suggestions.

In Eclipse "refreshment" is needed. Close and open the project, clean the project, etc. With Maven+Eclipse the reason could be wrong path to Main class given in the POM file.

If the error is just "Could not find or load main class", something's messed up with the environment.

I also ran into same issue. by default JRE libraries are also added under libraries tab in "Buildpath panel of project properties in eclipse. still it was giving me same error when I build application as standalone java application. But later I found that JRE is not checked under "Order and Export" tab (next to the libraries tab). So, I just checked it and build application. it is working now.

原因之一可能是:错误地添加了多个版本的JRE系统库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM