如何使用命令行运行maven创建的jar文件

Ouj*_*ujk 51 java jar maven

我需要一些帮助尝试使用命令行运行以下maven项目:https: //github.com/sarxos/webcam-capture,webcam-capture-qrcode示例是我正在尝试运行的那个.我使用Eciplse IDE运行它,但需要将其移动到仅使用命令行.我有由maven创建的jar.

我尝试着

java -classpath ./webcam-capture/target/webcam-capture-0.3.10-SNAPSHOT.jar  com.github.sarxos.webcam.WebcamQRCodeExample      
Run Code Online (Sandbox Code Playgroud)

但我一直在接受

Exception in thread "main" java.lang.NoClassDefFoundError: com/github/sarxos/webcam/WebcamQRCodeExample
Caused by: java.lang.ClassNotFoundException: com.github.sarxos.webcam.WebcamQRCodeExample
Run Code Online (Sandbox Code Playgroud)

mab*_*aba 108

只需使用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>
Run Code Online (Sandbox Code Playgroud)

然后你运行程序:

mvn exec:java
Run Code Online (Sandbox Code Playgroud)

  • 我应该在哪里添加?到主项目pom.xml? (3认同)

Moh*_*put 15

第1步:在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>
Run Code Online (Sandbox Code Playgroud)

第二步:逐行执行此命令.

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
Run Code Online (Sandbox Code Playgroud)


Haf*_*Ali 10

使用此命令.

mvn package
Run Code Online (Sandbox Code Playgroud)

制作包jar文件.然后,运行此命令.

java -cp target/artifactId-version-SNAPSHOT.jar package.Java-Main-File-Name
Run Code Online (Sandbox Code Playgroud)

在mvn package命令之后.将创建具有类,测试类,jar文件和其他资源文件夹和文件的目标文件夹.

键入您自己的artifactId,版本和包以及java主文件.

  • 当 jar 文件具有依赖项时,仅此方法将不起作用。maba 建议的 Maven exec 插件将_查找_依赖项,而 Mohammed 建议的 Maven Shadow 插件将_打包_它们。 (2认同)

Sha*_*ena 5

我不确定你的情况。但据我所知,要从 cmd 运行任何 jar 文件,我们可以使用以下命令:

转到保存 jar 文件的目录:

java -jar <jarfilename>.jar
Run Code Online (Sandbox Code Playgroud)

但您可以检查以下链接。我希望它能帮助你:

从命令行运行 Netbeans maven 项目?

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

  • Maven jar 不包含依赖项 (7认同)
  • 仅当罐子中有清单时才有效;在这种情况下没有。不过还是谢谢你。 (6认同)