2017-03-09 55 views
0

我在jar中的maven上构建项目后遇到问题。Maven构建错误:无法找到或加载主类

我把包和创建.jar文件:

mvn package 

后,我跑我的jar文件

java -jar artifactId.jar 

,并得到错误:

Error: Could not find or load main class com.emercit.app.App 

如果我运行目标类目录中,我收到类似的错误:

cd target/classes/com/emercit/app 
java App 
Error: Could not find or load main class App 

我的代码在App类:在pom.xml中

package com.emercit.app; 

public class App extends Application { 

    /** 

    Program code 

    **/ 

    } 

public static void main (String[] args) { 


    //Init form 
    Thread myThready = new Thread(() -> { 
     launch(args); 
    }); 
    myThready.start(); 

    } 
} 

我的生成属性:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 


     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>false</addClasspath> 
         <classpathPrefix>lib/</classpathPrefix> 
         <mainClass>com.emercit.app.App</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>copy</id> 
        <phase>install</phase> 
        <goals> 
         <goal>copy-dependencies</goal> 
        </goals> 
        <configuration> 
         <outputDirectory> 
          ${project.build.directory}/lib 
         </outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
+0

你能不能分享github链接? – Reborn

+0

是不是你的主要()课外? –

+0

https://github.com/sterkhov/BuildError – Sterkhov

回答

0

建立你的罐子这个插件,并指定主类,这将增加正确的META -INF在你生成的jar里。

<plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
       <version>3.0.0</version> 
       <executions> 
        <execution> 
         <phase>package</phase> 
         <goals> 
          <goal>shade</goal> 
         </goals> 
         <configuration> 
          <transformers> 
           <transformer 
             implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
            <manifestEntries> 
             <Main-Class>com.barclaycard.blackboxtester.Application</Main-Class> 
             <Build-Number>123</Build-Number> 
            </manifestEntries> 
           </transformer> 
          </transformers> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
0

您的设置与我的操作类似,只是您需要将POM文件中的此参数更改为“true”的值。

<addClasspath>true</addClasspath> 

没有它,你将不得不在命令行中包含依赖关系,你不是。 Java不会因为它们在目录中而找到jar。该命令将使用JAR在MANIFEST.MF文件中添加“classpath”。

生成的MANIFEST.MF文件将包含类似这样的行(取自我的一个开源项目)。你的jar依赖关系当然会有所不同。

Class-Path: calendar-2.2.3.jar algebrain-2.2.3.jar commons-codec-1.9.j 
ar argument-4.3.8.jar slf4j-api-1.7.13.jar slf4j-log4j12-1.7.13.jar l 
og4j-1.2.17.jar 
0

文件结构而产生的任何Maven化项目应该的src/main/java的和包装应在类文件中包括 其他明智它会抛出错误无法找到或加载主类

相关问题