3

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

  • 1
    Have you seen [this](https://stackoverflow.com/questions/10568275/noclassdeffounderror-on-maven-dependency) post? – Raman Sahasi Sep 17 '18 at 12:06
  • 2
    Your class path should include all of your dependencies (unless you are using some bundling / fat jar so that all of your dependencies reside in a single jar). To create a folder with all of your dependencies use dependency:copy-dependencies – Roy Shahaf Sep 17 '18 at 12:07
  • 1
    Possible duplicate of [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Alex Shesterov Sep 17 '18 at 12:13
  • 1
    Possible duplicate of [NoClassDefFoundError on Maven dependency](https://stackoverflow.com/questions/10568275/noclassdeffounderror-on-maven-dependency) – RobC Sep 17 '18 at 14:44

2 Answers2

2

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

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
2

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>
Rahim Dastar
  • 1,259
  • 1
  • 9
  • 15