74

I need some help trying to run the following maven project using the command line: https://github.com/sarxos/webcam-capture, the webcam-capture-qrcode example is the one I'm trying to run. I have it running using an the Eciplse IDE but need to move it over to just using the command line. I have the jar's created by maven.

I'm trying

java -classpath ./webcam-capture/target/webcam-capture-0.3.10-SNAPSHOT.jar  com.github.sarxos.webcam.WebcamQRCodeExample      

but I keep getting the

Exception in thread "main" java.lang.NoClassDefFoundError: com/github/sarxos/webcam/WebcamQRCodeExample
Caused by: java.lang.ClassNotFoundException: com.github.sarxos.webcam.WebcamQRCodeExample
Oujk
  • 771
  • 1
  • 5
  • 6

4 Answers4

148

Just use the exec-maven-plugin.

<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>

Then you run you program:

mvn exec:java
mss
  • 1,804
  • 1
  • 17
  • 18
maba
  • 47,113
  • 10
  • 108
  • 118
21

1st Step: Add this content in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2nd Step : Execute this command line by line.

cd /go/to/myApp
mvn clean
mvn compile 
mvn package
java -cp target/myApp-0.0.1-SNAPSHOT.jar go.to.myApp.select.file.to.execute
Mohammed Saqib Rajput
  • 1,331
  • 1
  • 14
  • 22
16

Use this command.

mvn package

to make the package jar file. Then, run this command.

java -cp target/artifactId-version-SNAPSHOT.jar package.Java-Main-File-Name

after mvn package command. Target folder with classes, test classes, jar file and other resources folder and files will be created.

type your own artifactId, version and package and java main file.

Hafiz Shehbaz Ali
  • 2,566
  • 25
  • 21
  • i used this method and i'm getting 'could not find or load main' error. I ran this: java -cp TwitterMapReduce-1.0-SNAPSHOT.jar hadoop.TwitterJob after doing mvn package. Any ideas why? – Hana Mar 16 '19 at 02:50
  • 5
    This alone will not work when the jar file has dependencies. The Maven exec plugin suggested by maba will _find_ the dependencies, while the Maven shade plugin suggested by Mohammed will _package_ them. –  Sep 12 '19 at 12:43
4

I am not sure in your case. But as I know to run any jar file from cmd we can use following command:

Go up to the directory where your jar file is saved:

java -jar <jarfilename>.jar

But you can check following links. I hope it'll help you:

Run Netbeans maven project from command-line?

http://www.sonatype.com/books/mvnref-book/reference/running-sect-options.html

Community
  • 1
  • 1
Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28