Search code examples
javaseleniummavenintellij-ideajar

Why does Selenium work for me in IntelliJ but fail when I run my jar in the command line?


I set up Selenium version 4.7.1 in my project in IntelliJ using maven. In order to get past the error reported here, I added an explicit dependency for guava version 31.1 (which is what is included in the lib folder of the downloadable package on the Selenium website for that version) and I added an exclusion on the selenium dependency for guava.

Now the application works fine when I run it in IntelliJ, but I'm getting this error when I run the jar in my terminal:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
...
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)

Other similar questions I've found, like this one, all focus on the guava issue, but it seems like at this point my problem must have something to do with my build configuration in IntelliJ and the jar that is getting created.

I did find some discussions that seemed more pertinent to my issue, but they were geared towards people manually adding the jars, and the advice given was usually to switch to use maven or gradle, but I'm already using maven. I have gone to Project Settings > Libraries and removed any external jars so that all dependencies should be getting pulled in by Maven.

Another discussion I found suggested using the maven-shaded-plugin, which I added and did mvn clean install, but still no luck.

How can I make sure the jar created by IntelliJ runs exactly the same as it runs in IntelliJ? Any help is appreciated.


Here is my current pom.xml, which includes a suggestion from the current answer, but at this point I'm still experiencing the same issue:

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>stock-analysis</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.github.sh0nk</groupId>
        <artifactId>matplotlib4j</artifactId>
        <version>0.5.0</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.6</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-openfeign-core</artifactId>
    <version>3.0.2</version>
  </dependency>
  <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
  </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.8.0</version>
    <scope>test</scope>
  </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.1-jre</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.7.2</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>

            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
                    <packageName>main.java.termaat.stocks.strategy.processing</packageName>
                    <mainClass>StockReportWriter</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <includeScope>compile</includeScope>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

Here is a screenshot of my project structure after the updates to my pom. Currently the stock-analysis-1.0-SNAPSHOT.jar that is built is much to small, so I'm trying to figure out if something is configured wrong with the maven-jar-plugin.

enter image description here


Solution

  • Probably you aren't building dependencies correctly. U need both:

    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
    
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
                                <packageName>your package</packageName>
                                <mainClass>your class</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
    
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                <includeScope>compile</includeScope>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    It's hard to help if you don't post your pom.xml though