2013-04-08 67 views
38

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

我想

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 

回答

2

我不知道你的情况。但是,当我知道从运行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

+2

只有在罐子里有清单才有效;在这种情况下没有。谢谢你。 – Oujk 2013-04-08 05:12:08

+0

maven jar不包含依赖关系 – 2016-04-02 19:34:48

77

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

我应该在哪里添加它?到主项目pom.xml中? – Oujk 2013-04-08 15:01:00

+0

@Oujk是的,到pom文件 – maba 2013-04-08 15:44:10

+0

哪一个?项目中几乎每个目录都有一个pom.xml。 – Oujk 2013-04-08 18:41:32

11

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

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

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 
相关问题