2017-08-16 55 views
0

后无法找到或加载主类,当我设置我的pom.xml时,我的应用程序仍然无法运行,说它无法找到或加载主类。 我安装我的pom.xml作为陈述hereMaven在正确设置插件

我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.example</groupId> 
    <artifactId>EditPropertiesFile</artifactId> 
    <version>1.0</version> 
    <packaging>jar</packaging> 

    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>com.example.EditPropertiesFile.Main</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
       <executions> 
        <execution> 
         <id>make-assembly</id> 
         <phase>package</phase> 
         <goals> 
          <goal>single</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration --> 
     <dependency> 
      <groupId>commons-configuration</groupId> 
      <artifactId>commons-configuration</artifactId> 
      <version>1.6</version> 
     </dependency> 
    </dependencies> 

</project> 

我用命令MVN清洁编译组件:单将应用程序打包,然后用java的运行 - 罐子outputedJar.jar

这是MANIFEST.MF这是罐内,说:

Manifest-Version: 1.0 
Archiver-Version: Plexus Archiver 
Created-By: Apache Maven 
Built-By: xxxx 
Build-Jdk: 1.8.0_121 
Main-Class: com.example.EditPropertiesFile.Main 

然而,当我跑,我得到一个错误:

Error: Could not find or load main class com.example.EditPropertiesFile.Main

我不知道还有什么我可以试试,因为我有这么尝试了所有在各种答案,他们似乎都解决其他一些问题。

编辑: 输出运行时,命令:

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building EditPropertiesFile 1.0 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ EditPropertiesFile --- 
[INFO] Deleting D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\target 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ EditPropertiesFile --- 
[WARNING] Using platform encoding (Cp1250 actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\src\main\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ EditPropertiesFile --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-assembly-plugin:2.4:single (default-cli) @ EditPropertiesFile --- 
[WARNING] Cannot include project artifact: com.example:EditPropertiesFile:jar:1.0; it doesn't have an associated file or directory. 
[INFO] Building jar: D:\Users\xxxx\Documents\Java_workspace\EditPropertiesFile\target\EditPropertiesFile-1.0-jar-with-dependencies.jar 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.525 s 
[INFO] Finished at: 2017-08-16T21:17:56+02:00 
[INFO] Final Memory: 13M/225M 
[INFO] ------------------------------------------------------------------------ 

里面的jar文件没有文件Main.java

我的项目的结构:

project structure

回答

0

检查/提取jar文件并查找是否在jar文件中添加了Main类,并且检查包结构。

另外,您可以在执行您提到的mvn命令期间添加来自mvn的输出日志。

[更新] 看起来你的目录结构可能是一个问题,所以检查它是否正确。例如,在这种情况下,如果baseProjectDir是您拥有pom.xml的目录(baseProjectDir \ pom.xml),那么您的Main.java应该位于相对于baseProjectdir的以下目录中: baseProjectDir \ src \ main \ java \ com \ example \ EditPropertiesFile \ Main.java

+0

我现在编辑的问题。 Main.java不在我输出的jar中。 –

+0

我不确定目录结构是否是问题。我编辑了问题,在最后给出了项目结构图 –

+0

,如果您使用命令行中的mvn,那么您可能需要确保目录结构符合mvn默认值。也许你可以尝试一下我刚才提到的结构,如果还没有完成的话。 – Sharman25

0

如果主类是EditPropertiesFile删除。主要

也添加到pom.xml中:

<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"> 
        <mainClass>com.example.EditPropertiesFile.Main</mainClass> 
       </transformer> 
      </transformers> 
     </configuration> 
    </execution> 
</executions> 

+0

不,com.example.EditPropertiesFile包含主类Main.java。我已经在我的pom文件中包含了所有内容(除了最终名称,这不是问题) –