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

2022-08-31 13:46:53

我需要一些帮助来尝试使用命令行运行以下maven项目:https://github.com/sarxos/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

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

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

推荐