简体   繁体   中英

Error: Could not find or load main class in maven project

I am facing a strange issue.I am getting " Error: Could not find or load main class " in basic hello world program in my maven project whenever I've add selenium 3x jar files in pom.xml There is no such issue in 2.53 selenium jar files or adding any other dependencies. This issue occurs only with adding this specific jar only. I'am using Eclipse (Oxygen) march version. Java version in system"1.8.0_102"

public class hello2 {

    public static void main(String[] args) {
        System.out.println("hello");
    }
}

My maven dependencies

<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>com.test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.5.3</version>
</dependency>

  </dependencies>
</project>

problems

I am not sure if this is the actual cause of the issue, but for maven to add the information about the main class to the jar's manifest, you must tell maven explicitly to do so. I use the maven-assembly-plugin for that. It packages all dependencies and my code into one runnable jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <mainClass>com.example.package.MainClass</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

It is not clear from your question why you need selenium 3x jars as your code doesn't require any selenium jar . Moreover, the errors in details would have helped us to debug your issue in a better way.

However, 2 (two) issues are pretty evident as follows :

  • You are using JDK 1.8.0_102 which is pretty ancient.
  • You are using org.seleniumhq.selenium v 3.5.3 which is pretty old.
  • From the snapshot it seems possibly the .m2/repository have got corrupted due to presence of multiple jars from Selenium 2.53 and Selenium 3.5.3 repository.

Solution

  • Upgrade JDK to recent levels JDK 8u171 .
  • If you don't have any dependency on Selenium remove the selenium jar dependency completely.
  • If you have a dependency on Selenium add the latest selenium dependency as follows :

     <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>3.12.0</version> </dependency> 
  • If the .m2/repository have got corrupted delete the .m2 sub-directory completely.

  • From the console execute : mvn clean (to flush out previous dependencies)
  • From the console execute : mvn install (to install the necessary dependencies)
  • From the console execute : mvn test (to execute your program)

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