【问题标题】:How to run a maven created jar file using just the command line如何仅使用命令行运行 Maven 创建的 jar 文件
【发布时间】:2013-03-29 23:38:26
【问题描述】:

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

我在努力

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

但我一直收到

Exception in thread "main" java.lang.NoClassDefFoundError: com/github/sarxos/webcam/WebcamQRCodeExample
Caused by: java.lang.ClassNotFoundException: com.github.sarxos.webcam.WebcamQRCodeExample

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    只需使用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>
    

    然后你运行你的程序:

    mvn exec:java
    

    【讨论】:

    • 我应该在哪里添加?到主项目pom.xml?
    • @Oujk 是的,到 pom 文件
    • 哪一个?项目中几乎每个目录都有一个 pom.xml。
    • @Oujk 通常是你的主要课程所在的那个。在你的情况下,你有 com.github.sarxos.webcam.WebcamQRCode。依赖关系将像往常一样由 maven 解决。
    • 最后一个 mvn 命令应该在哪里执行?我的系统可能没有安装 maven。我需要在没有更多信息的情况下为客户分发那个 jar。
    【解决方案2】:

    第一步:在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>
    

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

    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
    

    【讨论】:

      【解决方案3】:

      使用此命令。

      mvn package
      

      制作打包的jar文件。然后,运行此命令。

      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

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

      【讨论】:

      • 我使用了这种方法,但出现“无法找到或加载主程序”错误。我运行了这个: java -cp TwitterMapReduce-1.0-SNAPSHOT.jar hadoop.TwitterJob 在做 mvn 包之后。任何想法为什么?
      • 当 jar 文件具有依赖项时,仅此一项将不起作用。 maba 推荐的 Maven exec 插件会找到依赖,而 Mohammed 推荐的 Maven shade 插件会打包它们。
      【解决方案4】:

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

      转到保存jar文件的目录:

      java -jar <jarfilename>.jar
      

      但您可以查看以下链接。希望对你有帮助:

      Run Netbeans maven project from command-line?

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

      【讨论】:

      • 只有在 jar 中有 manifest 时才有效;在这种情况下,没有。不过谢谢。
      • maven jar 不包含依赖项
      猜你喜欢
      • 2021-07-16
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 2019-08-11
      • 2016-11-07
      • 2012-06-29
      • 2021-09-18
      • 2014-04-01
      相关资源
      最近更新 更多