How to run a maven created jar file using just the command line

JavaMavenJar

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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:

https://stackoverflow.com/questions/3689020/run-netbeans-maven-project-from-command-line

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOujkView Question on Stackoverflow
Solution 1 - JavamabaView Answer on Stackoverflow
Solution 2 - JavaMohammed Saqib RajputView Answer on Stackoverflow
Solution 3 - JavaHafiz Shehbaz AliView Answer on Stackoverflow
Solution 4 - JavaShailesh SaxenaView Answer on Stackoverflow