2017-08-16 56 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文件中包含了所有內容(除了最終名稱,這不是問題) –