3

I'm have to run maven project in cmd. This is my pom.xml file:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>

<name>my-app</name>
<!-- FIXME change it to the project's website -->

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.release>11</maven.compiler.release>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.16</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.2.Final</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

The project is running perfectly fine in IDE (Eclipse), but when I try to run it run it from command line I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
        at com.mycompany.app.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more
Slabgorb
  • 786
  • 6
  • 17
  • 3
    You build your project without dependencies. You also need to add dependencies into your jar. Look at this article https://www.baeldung.com/executable-jar-with-maven, section 2.2. – HereAndBeyond Jun 25 '19 at 18:44

2 Answers2

1

The problem in your case is that you dont have the dependencies in the classpath. You can use a maven pluging to obtain such.

 <build>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>fully.qualified.MainClass</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
              <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                   <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
      </plugins>
    </build>

Source How can I create an executable JAR with dependencies using Maven?

Alexander Petrov
  • 9,204
  • 31
  • 70
1

It really depends on how you setup your project, but what your stack trace is saying literally that JVM could not locate mentioned class. Now, you can imagine (this is just only one of many possible issues) - you have your code referencing 'Configuration' class somewhere, but when the project runs from a command line - JVM can not find it.

I would recommend taking a look at this topic to understand received error. Many thoughts were given and it was explained well.

Essentially once you get all your dependencies in place, that error will goaway and if you're using TestNG or JUnit, you won't need a main class. You will run your tests with just mvn clean test Or even shorter mvn if you add <defaultGoal>clean test</defaultGoal> to POM.

Mykola
  • 352
  • 1
  • 9