java.lang.NoClassDefFoundError when running on maven

7.6k Views Asked by At

I am running the following commands from here

mvn package

The package is compiling sucessfullly.

But when I am running

java -cp target/cloak-1.0-SNAPSHOT.jar com.github.cloak.App 

It is giving the following error

Error: Unable to initialize main class com.github.cloak.App

Caused by: java.lang.NoClassDefFoundError: boofcv/gui/image/ImagePanel

Am I compiling the wrong way?

EDIT: I am not using eclipse

UPDATE: https://stackoverflow.com/a/52367511/5699915

2

There are 2 best solutions below

0
On

use mvn exec:java

    <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>com.example.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

see this post

https://stackoverflow.com/a/15872962/1484621

0
On

One way is to create fat jar with all dependencies.Assembly plugin is the simplest, add these lines to pom.xml and repackage it.

 <build>
  <plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                    <manifest>
                        <mainClass>com.github.cloak.App</mainClass>
                    </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>